How to unwire an event handler?

I have limited understanding of delegates and event handlers.  I know that I can have:

button.Click += new EventHandler(mybutton_Click);

and also:

button.Click -= new EventHandler(mybutton_Click);

I believe the second statement will unwire the event handler that has been wired by the first statement.  My questions are:

a.  What does the new keyword in the second statement mean, when I am actually trying to remove something

b.  How do I unwire an event handler without knowing its name   That is, I want to unwire whatever has been wired to button.Click and start afresh with a new event handler. ( Or better still, how do I unwire the top most handler for that object )

I tried:

button.Click = null;

but it didn't work

My application has objects which are wired up at run time so it is not always possible to know at design time what has been wired.

Thanks.



Answer this question

How to unwire an event handler?

  • George102ci

    Assuming you have a form or control wired up to events and want to unwire them when the form/control is "closed" and you want it to be available for disposing ... what's best practice where the unwiring should happen OnHandleDestroyed

    Thanks,

    Tom

  • Qadian

    Thanks. 

    Your following is interesting.

     Anson Horton wrote:

    Another approach this is to store away the actual delegate instance such that you can later remove it.  For example:

    EventHandler myClick = new EventHandler(buttonClick);

    button1.Click += myClick;

    button1.Click -= myClick;

     

    Will it work if I do:

      EventHandler myClick = new EventHandler(buttonClick);

      button1.Click += myClick;

      button2.Click += myClick;

      ....

      button2.Click -= myClick;

      button1.Click -= myClick;

    and how different is it, in terms of memory allocation or whatever, from doing it the traditional way


  • Dave Bettin

    Each button will have its own invocation list. What you are doing is adding or removing your event handler to / from 2 different lists.
  • Hermes 68

    Hello,

    In both of these statements the new keyword means the same thing, allocate an instance of the EventHandler delegate whose invocation list consists of the single method mybutton_Click.  The confusing aspect is how delegate equality works.  When the -= operator is called, a search is done through the invocation list and the delegate that appears last in the invocation list that is equal to the delegate that you pass in is the one that is removed.  The C# language specification states that two delegates are equal if the instance and method that they point to are the same; however, it's ambiguous about whether or not that is true if the delegate type is different.  So, depending on the particular implementation of the runtime this may or may not work:

    delegate void MyEventHandler(object sender, EventArgs e);

    button1.Click += new EventHandler(buttonClick);

    button1.Click -= new MyEventHandler(buttonClick);

    Another approach this is to store away the actual delegate instance such that you can later remove it.  For example:

    EventHandler myClick = new EventHandler(buttonClick);

    button1.Click += myClick;

    button1.Click -= myClick;

    That same approach can be used to answer your second question.  When you hook up a new method to an event at runtime simply store away the delegate in a list; when you want to remove all of the handlers on the event simply iterate through the list and call -= on each one.

    Hope that helps!
    Anson Horton
    Visual C# IDE PM

     

     



  • Dana Ballinger

    Yes, OnHandleDestroyed is a good place to do this.

  • How to unwire an event handler?