469,352 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How to clear up all attached event handlers.

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.
Jun 13 '07 #1
5 24790
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.

"zlf" <zl***@hotmail.comwrote in message
news:eP*************@TK2MSFTNGP02.phx.gbl...
>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.

Jun 13 '07 #2
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

"Daniel Cigic" <da**********@gmail.com写入消息新闻:ur************ **@TK2MSFTNGP03.phx.gbl...
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.

"zlf" <zl***@hotmail.comwrote in message
news:eP*************@TK2MSFTNGP02.phx.gbl...
>>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.


Jun 13 '07 #3
Daniel Cigic <da**********@gmail.comwrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 13 '07 #4
zlf <zl***@hotmail.comwrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 13 '07 #5
Thanks Jon, I thought that zlf was "in the class" and in that case it would
be possible to get invocation list.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Daniel Cigic <da**********@gmail.comwrote:
>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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 13 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by tony kulik | last post: by
1 post views Thread by MuZZy | last post: by
3 posts views Thread by Peter Oliphant | last post: by
13 posts views Thread by Charles Law | last post: by
7 posts views Thread by Michael D. Ober | last post: by
2 posts views Thread by Claire | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.