473,812 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14352
You can only remove event-handlers that you know about. For example, if you
have:

void SomeHandler(obj ect sender, KeyPressEventAr gs e) {...}

then you can use:

t.KeyPress -= SomeHandler;
or (identical)
t.KeyPress -= new KeyPressEventHa ndler(SomeHandl er);

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:

KeyPressEventHa ndler 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.GetInvo cationList())
{
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...@g mail.comwrote:
You can only remove event-handlers that you know about. For example, if you
have:

void SomeHandler(obj ect sender, KeyPressEventAr gs e) {...}

then you can use:

t.KeyPress -= SomeHandler;
or (identical)
t.KeyPress -= new KeyPressEventHa ndler(SomeHandl er);

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:

KeyPressEventHa ndler 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
1942
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 datagrids using the ItemCreated event, template colums and the Button.Attributes.Add ("onclick", "return confirm('Are you sure you wish to delete this row?');"). This works fine in some instances when you click on the delete button but not always.
0
2326
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 datagrids using the ItemCreated event, template colums and the Button.Attributes.Add ("onclick", "return confirm('Are you sure you wish to delete this row?');"). This works fine in some instances when you click on the delete button but not always.
4
3911
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 think my logic seems fine but when I click the button on my datagrid nothing seems to happen. Have you any idea where Im going wrong. Was thinking it might have something to do with my page load/ postback but not really sure. Can someone please...
4
334
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 think my logic seems fine but when I click the button on my datagrid nothing seems to happen. Have you any idea where Im going wrong. Was thinking it might have something to do with my page load/ postback but not really sure. Can someone please...
10
2976
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 22222 D E F 33333 G H I 44444 J K L
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10417
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7677
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.