How to clear up all attached event handlers.

Z

zlf

I have an UserControl created by other component, its creator attachs some
event handlers to MouseDoubleClick event, but I do not like those events to
be triggered while it is db-clicked. I want to know how to clear up them?
Thanks

BTW: I do not know what specific event handlers are attached, so I cannot
use Banner_MouseDoubleClick -= XXX to unregister it.
 
D

Daniel Cigic

If you want to clear all you can just assign null ot event ...
otherwise you can use
Delegate.GetInvocationList() and then Method & Target property to get
MethodInfo of each method.
 
Z

zlf

userControl.MouseDoubleClick = null leads to compile error.
The event 'System.Windows.Forms.Control.MouseDoubleClick' can only appear on
the left hand side of += or -=.
And I tried userControl.MouseDoubleClick += null, it does not clear up the
attached event handlers.

May u give a simple statement? Thx
 
J

Jon Skeet [C# MVP]

Daniel Cigic said:
If you want to clear all you can just assign null ot event ...
otherwise you can use
Delegate.GetInvocationList() and then Method & Target property to get
MethodInfo of each method.

Strictly speaking, you can't do any of these things for an event. You
*can* do them for delegates - the two are different.

Field-like events in C# (i.e. the ones you get with simple
public event EventHandler Foo;
) create both an event and a field of the relevant delegate type
(EventHandler here). Within the class, any reference to Foo is a
reference to the field. Outside the class, any reference to Foo is a
reference to the event.

Events only have add/remove, a bit like properties only have get/set.
In other words, you can't perform assignment on them, nor get their
invocation lists.

See http://pobox.com/~skeet/csharp/events.html for more information
 
J

Jon Skeet [C# MVP]

zlf said:
userControl.MouseDoubleClick = null leads to compile error.
The event 'System.Windows.Forms.Control.MouseDoubleClick' can only appear on
the left hand side of += or -=.
And I tried userControl.MouseDoubleClick += null, it does not clear up the
attached event handlers.

The point of an event is to encapsulate the event handling so that
callers from the outside can only subscribe and unsubscribe. If this
were a custom event you were creating in your user control, you could
expose the ability to clear it - but if it's an event inherited from
Control, then unless Control provides a way of clearing the event
handlers, you're out of luck. (You could use reflection potentially,
but that would be very fragile - any change to the implementation would
screw up your application.)
 
D

Daniel Cigic

Thanks Jon, I thought that zlf was "in the class" and in that case it would
be possible to get invocation list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top