Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How to delete an event handler

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dom - 20 Mar 2008 15:21 GMT
For various reasons, I need to remove the event handlers for a given
event.  The event can only appear on the left side of "+=" or "-=", so
I'm not sure how this is done.

I tried this:

while (t.KeyPress != null) t.KeyPress--;

but the compiler won't accept it.  Any ideas?

Dom
Marc Gravell - 20 Mar 2008 15:41 GMT
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
Sebastian Lopez - 20 Mar 2008 19:12 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.