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?

Re: Help on dynamically changing an event handler by Peter

Peter
Wed May 07 19:16:39 CDT 2008

Yes you do need to remove unwanted event handlers. Do this the same way as
you added it, except replace the "+=" with "-=".
Peter
"bjjnova" <bjjnova@gmail.com> wrote in message
news:d795b17a-d70d-4294-8c95-f13be358e900@a23g2000hsc.googlegroups.com...
>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?


Re: Help on dynamically changing an event handler by bjjnova

bjjnova
Wed May 07 19:38:47 CDT 2008

On May 7, 8:16=A0pm, "Peter Bromberg [C# MVP]"
<pbromb...@nospammaam.yahoo.com> wrote:
> Yes you do need to remove unwanted event handlers. Do this the same way as=

> you added it, except replace the "+=3D" with "-=3D".
> Peter"bjjnova" <bjjn...@gmail.com> wrote in message
>
> news:d795b17a-d70d-4294-8c95-f13be358e900@a23g2000hsc.googlegroups.com...
>
>
>
> >I need help dynamically resetting an event handler.
>
> > I have a form with several buttons. =A0I have intialized the Click event=

> > of a particular button thus on the form:
>
> > _button_3.Click +=3D 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 +=3D or -=3D?):
>
> > public EventHandler Button3Click{
> > get { ; }
> > set { _button_3.Click +=3D 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){
> > =A0 =A0 =A0 =A0 =A0 =A0if (some condition){
> > the_form.Button3Click +=3D new EventHandler(this.onButton3Click_A);
> > }else{
> > the_form.Button3Click +=3D new EventHandler(this.onButton3Click_B);
> > }
>
> > }
>
> > I also need to remove the event handlers, don't I?- Hide quoted text -
>
> - 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?

Re: Help on dynamically changing an event handler by Peter

Peter
Wed May 07 23:23:52 CDT 2008

On Wed, 07 May 2008 17:38:47 -0700, bjjnova <bjjnova@gmail.com> wrote:

> 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?

You don't. Only the class defining the event has access to the currently
subscribed delegates.

Since "get { return _button_3.Click; }" doesn't make any sense, maybe you
can try to explain more precisely what it is you are trying to do. Note
that as a general rule, code outside a class defining an event has no
access to the subscribers to the event. There would be a serious
encapsulation problem if it were otherwise.

You could write an event in your own class that provides functionality
like clearing existing subscribers, etc. but you can't retrofit an
existing class to do that. Your only option would be to wrap the event
management in something else (like a different event, or methods for the
purpose) and always use that for your subscribing and unsubscribing. Note
that even then, only those delegates you've subscribed yourself via the
wrapper would be subject to your manipulation.

Pete

Re: Help on dynamically changing an event handler by Ben

Ben
Thu May 08 14:33:37 CDT 2008

bjjnova wrote:
> 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.