Removing an event handler

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Grr... having a syntax issue here... what is the proper way to remove an
event handler from an event?

Here is the even I'm wanting to remove after a different action is taken....

cbSingleSelectBox.SelectedIndexChanged += new
EventHandler(cbSingleSelectBox_SelectedIndexChanged);

thanks!
-James
 
Here is the even I'm wanting to remove after a different action is taken....
cbSingleSelectBox.SelectedIndexChanged += new
EventHandler(cbSingleSelectBox_SelectedIndexChanged);

Unless I'm missing something obvious, you should be able to just use -=

cbSingleSelectBox.SelectedIndexChanged -= new
EventHandler(cbSingleSelectBox_SelectedIndexChanged);

It is OK to use a new EventHandler object in the removal step. Internally,
-= will use the Equals method to verify the object/method pair in the already
registered delegate match the pair in the new one you are using for removal.
 
Wow, it must have been a long day yesterday... I tried that several times
and it didn't appear that it was working, and since I was using the 'new'
keyword, I assumed it was creating a new instance, so it made sense to me (at
the time) that it wasn't working.

So, with fresh eyes, I tried it again this morning and...viola! it worked.

thanks much!
 
Back
Top