473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to delete an event handler

Dom
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

Mar 20 '08 #1
2 14263
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
Mar 20 '08 #2
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

On Mar 20, 11:41 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
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
Mar 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Richard Payne | last post by:
I am developing a web application that utilises multiple grids, panels, validation controls etc on the same page. I have added the appropriate code to pop up dialogs to confirm delete in...
0
by: Richard Payne | last post by:
I am developing a web application that utilises multiple grids, panels, validation controls etc on the same page. I have added the appropriate code to pop up dialogs to confirm delete in...
4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
10
by: amiga500 | last post by:
Hello, I have one basic simple question. When I have multiple records in the datagrid as follows: Code Product 1 Product 2 Product 3 11111 A B C...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.