unregistering events

  • Thread starter Saso Zagoranski
  • Start date
S

Saso Zagoranski

Hi!

How can I unregister all the events registered to a control?
I have seen a piece of code in another thread, which gets all the registered
handlers for a specific event.

Let's say I have a CustomTextBox : TextBox, which has quite a lot of
events...
Do I put unregistering code for all the possible events in the Dispose
method? Or is there another way?

Could someone please provide just a simple example of this?

Thank,
Saso
 
S

Saso Zagoranski

Thanks for answering!

First off... "The Supreme All-Knowing Guru" ?? :)

As far as I understand the articles they discuss unregistering the event
from the object, which handles the event...
e.g. If I have a Form and a TextBox I would have something like:
TextBox.KeyPressed += ...

and when I want to unregister it:
TextBox.KeyPressed -=

What I would like to know is, whether it is possible to unregister the event
from the TextBox... e.g. when I'm calling
the Dispose() method of the textbox I would like to unregister all the
events...
class TextBox
{
...
protected override void Dispose()
{
UnregisterEvents();
}
}

Thanks,
saso
 
W

William Ryan

Jeffery Richter of Wintellect..he's one of the founders of it and probably
one of the most impressive .NET guys going (IMHO).

As far as disposing...that's a good question. not sure off of the top of my
head, but an interesting proposition. Let me play with it and see what I
can come up with.

BTW, if you haven't read any of Jeffrey's stuff..he has one fo the best
books on .NET I've read..
(http://www.amazon.com/exec/obidos/tg/detail/-/0735614229/qid=1068333345/sr=
8-1/ref=sr_8_1/103-3171781-7147053?v=glance&n=507846) and his articles on
Delegates are like manna from Heaven...(
http://www.wintellect.com/resources/articles.aspx?authorID=3 )

I'll play with it and get back to you shortly...
 
S

Sherif ElMetainy

Hello

To unregister all events of a control, simply dispose the Events List

this.Events.Dispose();

This works for the control's inherited events. If your control has custom
events defined by you, either make sure they use the Events list or
unregister them yourself using the -= syntax.

To find out how to use the Events list for your own events read the
"Optimizing Event Implementation" section in the following page:
http://msdn.microsoft.com/library/d...-us/cpguide/html/cpcondefiningcustomevent.asp


Best regards,

Sherif
 
W

William Ryan

Sherif:

I think his question was on unregistering a single event, but I may be
wrong. Either way, you make an excellent point here b/c using
Events.Dispose gives you the ability to get rid of everything at once and
that can be a real time saver in many instances. I was thinking in single
event mode, but you often need to get rid of everything, and the method you
mention is definitely an elegant implementation.
 
N

Nagachandra Sekhar Grandhi

I just want to dispose all the events register for a button control. But I
didn't find Events property for that button.

How can i do that?

-chandu.
 

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