Removing event handlers
-
Is there any way to remove all the event handlers associated with buttons click event without using -= operator
-
Is there any way to remove all the event handlers associated with buttons click event without using -= operator
With an event you can use only += or -= operator. Actually keyword "event" is modifying a delegate for this behaviour.
-
Is there any way to remove all the event handlers associated with buttons click event without using -= operator
Hello, A suggestion I gave some time ago and got no feedback, and never tried it myselfe! But maybe it helps you. The control class has a member called Events, which is a list of EventHandler (System.ComponentModel.EventHandlerList). This class provides a method called "RemoveHandler". Please let me know if it works for you! All the best, Martin
-
Is there any way to remove all the event handlers associated with buttons click event without using -= operator
No there isn't. You can't set an event to be
null
or something for example, which is strange because it is initialized to benull
. Here's my way of going around it:public event EventHandler ExampleEvent;
public void UnHook() { Collection<EventHandler> ExampleHandlers = new Collection<EventHandler>(); foreach (Delegate d in this.ExampleEvent.GetInvocationList()) ExampleHandlers.Add(d as EventHandler); foreach (EventHandler e in ExampleHandlers) this.ExampleEvent-= e; }
I don't know if there's a better way for this, but this works for me...
Standards are great! Everybody should have one!
-
Hello, A suggestion I gave some time ago and got no feedback, and never tried it myselfe! But maybe it helps you. The control class has a member called Events, which is a list of EventHandler (System.ComponentModel.EventHandlerList). This class provides a method called "RemoveHandler". Please let me know if it works for you! All the best, Martin
Hi Martin, Thanks for the reply. Control class has a "protected" property called Events which I can't use in my code(which is a Outlook addin)becoz I am using CommandBarButton.
-
Hi Martin, Thanks for the reply. Control class has a "protected" property called Events which I can't use in my code(which is a Outlook addin)becoz I am using CommandBarButton.