Help on dynamically changing an event handler

B

bjjnova

I need help dynamically resetting an event handler.

I have a form with several buttons. I have intialized the Click event
of a particular button thus on the form:

_button_3.Click += new
EventHandler(the_controller.onButton3Click_NewEE);

On the same form, I am establishing a property so that I can reset the
Click event from another class (first part of my question: how do I
establish the get accessor since _button_3.Click has to be on the left
side of += or -=?):

public EventHandler Button3Click{
get { ; }
set { _button_3.Click += value; }
}

To dynamically change the event handler, I want to reset it from
another class containing my Click methods but I have to have the get
accessor (in this case, I want to reset the Click even on button 3
when I click on button 5):

public void onButton5Click(Object o, EventArgs e){
if (some condition){
the_form.Button3Click += new EventHandler(this.onButton3Click_A);
}else{
the_form.Button3Click += new EventHandler(this.onButton3Click_B);
}

}

I also need to remove the event handlers, don't I?
 
P

Peter Bromberg [C# MVP]

Yes you do need to remove unwanted event handlers. Do this the same way as
you added it, except replace the "+=" with "-=".
Peter
 
B

bjjnova

Yes you do need to remove unwanted event handlers. Do this the same way as
you added it, except replace the "+=" with "-=".











- Show quoted text -

Thanks Peter; can you tell me the answer to my other question? I
cannot write get { return _button_3.Click}: how do I write the get
accessor?
 
B

Ben Voigt [C++ MVP]

bjjnova said:
I need help dynamically resetting an event handler.

I have a form with several buttons. I have intialized the Click event
of a particular button thus on the form:

_button_3.Click += new
EventHandler(the_controller.onButton3Click_NewEE);

On the same form, I am establishing a property so that I can reset the
Click event from another class (first part of my question: how do I
establish the get accessor since _button_3.Click has to be on the left
side of += or -=?):

public EventHandler Button3Click{
get { ; }
set { _button_3.Click += value; }
}

To dynamically change the event handler, I want to reset it from
another class containing my Click methods but I have to have the get
accessor (in this case, I want to reset the Click even on button 3
when I click on button 5):

public void onButton5Click(Object o, EventArgs e){
if (some condition){
the_form.Button3Click += new EventHandler(this.onButton3Click_A);
}else{
the_form.Button3Click += new EventHandler(this.onButton3Click_B);
}

}

I also need to remove the event handlers, don't I?

Actually not.

One way of solving your problem is:

/* in constructor or InitializeComponent */
button_3.Click += ClickForwarder;

/* in class definition */
public EventHandler Button3Click{ get; set; }
void ClickForwarder(object sender, EventArgs e)
{
if (sender == button_3 && Button3Click != null) Button3Click(sender, e);
}


This way you never need to add/remove event handlers on button 3 itself, you
maintain your own multicast delegate variable containing user-supplied
handlers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top