You can only remove event-handlers that you know about. For example, if you
have:
void SomeHandler(object sender, KeyPressEventArgs e) {...}
then you can use:
t.KeyPress -= SomeHandler;
or (identical)
t.KeyPress -= new KeyPressEventHandler(SomeHandler);
if you have used an anomymous function/lambda, then you'll need to cache the
delegate instance prior to subscribing, and unsubscribe with the same:
KeyPressEventHandler handler = (sender, e) => {}
t.KeyPress += handler;
//...
t.KeyPress -= handler;
Marc
Dom,
You can remove all the event handlers with the following code:
public EventHandler MyEvent;
foreach (EventHandler eventHandler in
MyEvent.GetInvocationList())
{
MyEvent -= eventHandler;
}
In this snippet, you can use the -= operator as you get a reference to
each handler suscribed to the event. In the other hand, operator --
cannot be used with events.
I hope it helps,
Seba
> You can only remove event-handlers that you know about. For example, if you
> have:
[quoted text clipped - 16 lines]
>
> Marc
Peter Duniho - 20 Mar 2008 19:17 GMT
> You can remove all the event handlers with the following code:
>
[quoted text clipped - 4 lines]
> MyEvent -= eventHandler;
> }
No, not really. (Even ignoring the fact that you left out the "event"
keyword in your declaration of the event :) ).
That code will only work from within the class where the event is defined,
and of course in that case you could just set the event to null in that
case. No need to remove each one individually.
Pete