473,538 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing All Event Handlers

Hi,

Is there a way to remove all event handlers for a control's event? Say, i have a button and i want
to remove all button.Click events for it - i don't know how many of them was hooked to the event and
what are the functions hooked, but i need to make sure that i unwired all of them at once.

I could unwire event handlers if i knew the functions:

button.Click += new EventHandler(OnMyBtnClick); // Wire event handler
....
button.Click -= new EventHandler(OnMyBtnClick); // Unwire event handler

But again, i don't know which/how many event handlers are wired to the event.
Any ideas/comments would be highly appreciated!

Thank you
Andrey
Nov 17 '05 #1
1 55706
Directly no, in large part because you cannot simply set the event to null.

Indirectly, you could make the actual event private and create a property
around it that tracks all of the delegates being added/subtracted to it.

Take the following:

ArrayList delegates = new ArrayList();

private event EventHandler MyRealEvent;

public event EventHandler MyEvent
{
add
{
MyRealEvent += value;
delegates.Add(value);
}

remove
{
MyRealEvent -= value;
delegates.Remove(value);
}
}

public void RemoveAllEvents()
{
foreach(EventHandler eh in delegates)
{
MyRealEvent -= eh;
}
delegates.Clear();
}

In this code, we create a wrapper around MyRealEvent (which is what you
would actually trigger internally), so that anytime something is
added/subtracted to it, a list of those EventHandlers is updated accordingly.
When you finally need to clear out all of the events, simply call
RemoveAllEvents() (poorly named I know) which iterates through the list of
EventHandlers and removes each in turn from the real event.

Brendan
"MuZZy" wrote:
Hi,

Is there a way to remove all event handlers for a control's event? Say, i have a button and i want
to remove all button.Click events for it - i don't know how many of them was hooked to the event and
what are the functions hooked, but i need to make sure that i unwired all of them at once.

I could unwire event handlers if i knew the functions:

button.Click += new EventHandler(OnMyBtnClick); // Wire event handler
....
button.Click -= new EventHandler(OnMyBtnClick); // Unwire event handler

But again, i don't know which/how many event handlers are wired to the event.
Any ideas/comments would be highly appreciated!

Thank you
Andrey

Nov 17 '05 #2

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

Similar topics

5
1554
by: Charles Law | last post by:
I think I asked the wrong question last time, so I am starting a separate post to distinguish them. Take five classes: ClassA and ClassW...Z ClassA raises five events: Event1...5 ClassW...Z handle some combination of these events. For example ClassW handles Event1, 2 and 5
13
3482
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through...
16
2485
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some...
14
8580
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of...
0
1441
by: KeironNicholson | last post by:
Hello, Does anybody know if there is ANY way to either get a reference to or just remove event handlers that you didn't declare yourself? (I am writing a plugin, but want to change the functionality of some buttons etc.) I've gathered that this is at least difficult if not impossible from other posts, but if anyone has a horrendous hack I'd...
5
1800
by: gnassar | last post by:
Essentially my problem is that .NET 2005 is removing my event handlers. There's no real special things about my project, it just continually removes them all. It starts on the open of a solution. The screen flashes and immediately I can undo something. I look at the undo list and it says designer generated code. When I undo it, all of my...
1
1341
by: Armin Zingler | last post by:
Hi, I add event handlers to different events of objects of different type. In an array or arraylist, I want to store the information about which events I added. Later, I want to process the arraylist and detach all the event handlers. My problem is that I don't know which information to store. When removing the handlers in a loop, the most...
0
1287
by: khalid galal | last post by:
Hi, i am having a problem with removing event handlers, it is when creating nested event handlers (an event handler raising another event handler) where a part of the code is private void toolStripButton1_Click(object sender, EventArgs e) { this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); }...
5
3055
by: Lloyd Sheen | last post by:
Is there a way to get the event handlers such that I can cache the info about handlers for a particular control, remove the handlers, do some code and restore the cached event handlers in VB.NET (2008). I would rather do things this way than having to have a boolean flag to indicated to the handler to exit right away. Psuedo code: ...
0
7365
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7536
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. ...
0
7694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7287
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...
0
5829
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5226
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...
0
3358
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...
1
933
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
591
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...

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.