Event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas

    #1

    Event

    Hi there

    I build a class "ButtonClas s" with two private
    Buttons, "OK" And "Cancel".
    Now, I would like to use this class in a Windowsform for
    example three times.

    ButtonClass c1 = new ButtonClass();
    ButtonClass c2 = new ButtonClass();
    ButtonClass c3 = new ButtonClass();

    I assume the EventListener and its function is in the
    ButtonClass. How do I know know which Button was pressed,
    because I have just one Listener for each of the Buttons?

    Thanks for helping

    Thomas
  • Thomas

    #2
    Re: Event

    Hi Mikael

    Thank you for helping. I'll try it this evening.

    [color=blue]
    >-----Original Message-----
    >Hi Thomas!
    >
    >In your case your buttonclass would have to throw events[/color]
    that is picked up[color=blue]
    >by your main class.
    >
    >First of you need to create delegates that tells[/color]
    everyone what the event[color=blue]
    >should look like. Place this outside your class.
    >
    >public delegate void OkClickEventHan dler(object sender,[/color]
    EventArgs e);[color=blue]
    >public delegate void CancelClickEven tHandler(object[/color]
    sender, EventArgs e);[color=blue]
    >
    >public class ButtonClass
    >{
    > //Now inside your buttonclass you add the events so[/color]
    that they can be[color=blue]
    >fired.
    > public event EventHandler OkClick;
    > public event EventHandler CancelClick;
    >
    > //This is not really nessessary to have, but I[/color]
    usually have it anyway[color=blue]
    >since it
    > //keeps things simple and that you might want to[/color]
    change the event a bit[color=blue]
    >if
    > //you inherit from the ButtonClass
    >
    > protected virtual void OnOkClick(Event Args e)
    > {
    > if (OkClick != null) //If someone is ready to[/color]
    handle the event[color=blue]
    > {
    > OkClick(this, e); //Fire the event
    > }
    > }
    >
    > protected virtual void OnCancelClick(E ventArgs e)
    > {
    > if (CancelClick != null) //If someone is ready[/color]
    to handle the event[color=blue]
    > {
    > CancelClick(thi s, e); //Fire the event
    > }
    > }
    >
    > private void okButton_Click( object sender,[/color]
    System.EventArg s e) //The[color=blue]
    >basic event fired by clicking the button
    > {
    > OnOkClick(Event Args.Emtpy); //Try to fire the[/color]
    event if someone is[color=blue]
    >listening for it
    > }
    >
    > private void cancelButton_Cl ick(object sender,[/color]
    System.EventArg s e)[color=blue]
    > {
    > OnCancelClick(E ventArgs.Empty) ;
    > }
    >}
    >
    >In your main class you need to listen for the events[/color]
    that the buttonclass[color=blue]
    >fires. Unless you do this you will never know when a[/color]
    button i pressed. So in[color=blue]
    >your main class you need to do this
    >
    >ButtonClass bc01 = new ButtonClass();
    >bc01.OkClick += new EventHandler(bc 01_OkClick); //Start[/color]
    listening for ok[color=blue]
    >button click in buttonclass bc01
    >bc01.CancelCli ck += new EventHandler(bc 01_CancelClick) ;
    >
    >ButtonClass bc02 = new ButtonClass();
    >bc02.OkClick += new EventHandler(bc 02_OkClick);
    >bc02.CancelCli ck += new EventHandler(bc 02_CancelClick) ;
    >And so on.
    >
    >Finally you need a method that handles the events that[/color]
    has been fired[color=blue]
    >
    >private void bc01_OkClick(ob ject sender, EventArgs e)
    >{
    > //Do what you want to do if they press OK in bc01
    >}
    >
    >This way you know exactly which ButtonControl is[/color]
    throwing the event and[color=blue]
    >which button in the control also.
    >
    >I hope this helps you out. Personally I got tired of[/color]
    doing all these steps[color=blue]
    >so I made a smal event generator that does this stuff[/color]
    for me. Just copy and[color=blue]
    >paste, which is quite nice and timesaving :)
    >
    >//Mikael
    >
    >
    >
    >"Thomas" <thomas_koller@ freesurf.ch> wrote in message
    >news:0a9501c36 244$132adc20$a5 01280a@phx.gbl. ..[color=green]
    >> Hi there
    >>
    >> I build a class "ButtonClas s" with two private
    >> Buttons, "OK" And "Cancel".
    >> Now, I would like to use this class in a Windowsform[/color][/color]
    for[color=blue][color=green]
    >> example three times.
    >>
    >> ButtonClass c1 = new ButtonClass();
    >> ButtonClass c2 = new ButtonClass();
    >> ButtonClass c3 = new ButtonClass();
    >>
    >> I assume the EventListener and its function is in the
    >> ButtonClass. How do I know know which Button was[/color][/color]
    pressed,[color=blue][color=green]
    >> because I have just one Listener for each of the[/color][/color]
    Buttons?[color=blue][color=green]
    >>
    >> Thanks for helping
    >>
    >> Thomas[/color]
    >
    >
    >.
    >[/color]

    Comment

    • Prateek

      #3
      Re: Event

      Hi,

      I maybe wrong here but from what I gather you have a class called
      ButtonClass and this class implements the click event. Now, you have created
      three ButtonClass objects and want to know which button was pressed in the
      click event handler.. I assume you can use the this keyword in the
      ButtonClass which will refer to the current object that raised the click
      event...

      Again, I may have made a mistake in understanding your query..

      -Prateek

      "Thomas" <thomas_koller@ freesurf.ch> wrote in message
      news:0a9501c362 44$132adc20$a50 1280a@phx.gbl.. .
      Hi there

      I build a class "ButtonClas s" with two private
      Buttons, "OK" And "Cancel".
      Now, I would like to use this class in a Windowsform for
      example three times.

      ButtonClass c1 = new ButtonClass();
      ButtonClass c2 = new ButtonClass();
      ButtonClass c3 = new ButtonClass();

      I assume the EventListener and its function is in the
      ButtonClass. How do I know know which Button was pressed,
      because I have just one Listener for each of the Buttons?

      Thanks for helping

      Thomas


      Comment

      Working...