473,507 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can a eventhandler delegate remove itself from an event without knowing the event?

For test purposes I attach an event to a control, say a TextBox
TextChanged event. At another time the same event delegate is attached
to some other control, maybe a listbox.

Same event function every where. The event function should happen only
once for that control (and then, maybe again if it is attached to the
control again).

Could I make that deattachment operation general? could the function
find out what event it is attaced? Something like
Event e = ????????;
Eventhandler ThisHandler = ????????;
e -= ThisHandler;

Any suggestions? Thanks in advance!
Best regards
Torben
Nov 16 '05 #1
6 7622
Hi Torben,

Yes, you can deattach the event handler. Standard .NET events when triggered
pass the object that triggered the event as well in the sender parameter.
All you need to do is cast the sender as your control then deatach the event
handler. Check the code below:

private void button2_Click(object sender, System.EventArgs e)
{
Control ctrl = (Control) sender;
ctrl.Click -= new System.EventHandler(this.button2_Click);
}
hope this helps

Fitim Skenderi

"Torben" <to****@superusers.dk> wrote in message
news:c3**************************@posting.google.c om...
For test purposes I attach an event to a control, say a TextBox
TextChanged event. At another time the same event delegate is attached
to some other control, maybe a listbox.

Same event function every where. The event function should happen only
once for that control (and then, maybe again if it is attached to the
control again).

Could I make that deattachment operation general? could the function
find out what event it is attaced? Something like
Event e = ????????;
Eventhandler ThisHandler = ????????;
e -= ThisHandler;

Any suggestions? Thanks in advance!
Best regards
Torben

Nov 16 '05 #2

Hi Fitim
Thank you very much for the answer. I like it, but it would be better if
I didn't have to know the type of event, since this code should work
very generally.

As in yout example:
ctrl.Click -= new System.EventHandler(this.button2_Click);

Here I need to know that the handler is attached to a Click event. What
if the control was a TextBox or something I don't know yet?

I just want to deattach from any event on the control

Best regards
Torben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Hi Torben,

To be honest I don't think you can do something like that. The delegates
(events) are just function pointers in a sense, and can have different
parameters. I think the only way for something "truly generic" is to user
Reflection and go through all event handlers and detach the code from the
ones you are interested.

Fitim Skenderi
"Torben Neesgaard" <to****@superusers.dk> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...

Hi Fitim
Thank you very much for the answer. I like it, but it would be better if
I didn't have to know the type of event, since this code should work
very generally.

As in yout example:
ctrl.Click -= new System.EventHandler(this.button2_Click);

Here I need to know that the handler is attached to a Click event. What
if the control was a TextBox or something I don't know yet?

I just want to deattach from any event on the control

Best regards
Torben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4
Fitim,
I think that you´re right - unfortunally!

Mvh Torben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5

Let's say that you have a common event handler delegate for more than one event. You can declare it separately (ex.):
System.EventHandler commonHandler;
commonHandler= new System.EventHandler(this.CommonHandler);

Associate it:
this.button1.Click += commonHandler;
this.button2.Click += commonHandler;
Remove it from all the controls:
foreach (Control innerControl in this.Controls)
{
System.Reflection.EventInfo[] evinfo = innerControl.GetType().GetEvents();
for (int index=0; index<evinfo.Length; index++)
{
evinfo[index].RemoveEventHandler(innerControl,commonHandler);
}
}
--
Horatiu Ripa

"Torben" <to****@superusers.dk> wrote in message news:c3**************************@posting.google.c om...
For test purposes I attach an event to a control, say a TextBox
TextChanged event. At another time the same event delegate is attached
to some other control, maybe a listbox.

Same event function every where. The event function should happen only
once for that control (and then, maybe again if it is attached to the
control again).

Could I make that deattachment operation general? could the function
find out what event it is attaced? Something like


Event e = ????????;
Eventhandler ThisHandler = ????????;
e -= ThisHandler;

Any suggestions? Thanks in advance!
Best regards
Torben

Nov 16 '05 #6

Let's say that you have a common event handler delegate for more than one event. You can declare it separately (ex.):
System.EventHandler commonHandler;
commonHandler= new System.EventHandler(this.CommonHandler);

Associate it:
this.button1.Click += commonHandler;
this.button2.Click += commonHandler;
Remove it from all the controls:
foreach (Control innerControl in this.Controls)
{
System.Reflection.EventInfo[] evinfo = innerControl.GetType().GetEvents();
for (int index=0; index<evinfo.Length; index++)
{
evinfo[index].RemoveEventHandler(innerControl,commonHandler);
}
}
--
Horatiu Ripa

"Torben" <to****@superusers.dk> wrote in message news:c3**************************@posting.google.c om...
For test purposes I attach an event to a control, say a TextBox
TextChanged event. At another time the same event delegate is attached
to some other control, maybe a listbox.

Same event function every where. The event function should happen only
once for that control (and then, maybe again if it is attached to the
control again).

Could I make that deattachment operation general? could the function
find out what event it is attaced? Something like


Event e = ????????;
Eventhandler ThisHandler = ????????;
e -= ThisHandler;

Any suggestions? Thanks in advance!
Best regards
Torben

Nov 16 '05 #7

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

Similar topics

2
2655
by: Sandor Heese | last post by:
Question, When using BeginInvoke (on a From to marshal to the UI thread) with the EventHandler delegate I see some strange behaviour. The problem is that I call the UpdateTextBox method with a...
3
3328
by: Matias Szulman | last post by:
Hi! I need to know if an event (i.e. button1_click) has an eventhandler associated. Is this possible in c#? Thanks, Matias
5
624
by: Torben | last post by:
For test purposes I attach an event to a control, say a TextBox TextChanged event. At another time the same event delegate is attached to some other control, maybe a listbox. Same event function...
5
2767
by: JMWilton | last post by:
Is there a way to determine what has been added to the eventhandler list? I have code that uses += and -= to turn event handling code on and off. Apparently, it is possible to add the same event...
9
3068
by: Christopher Weaver | last post by:
Can anyone tell me how I could iterate through a collection of controls on a form while assigning their event handlers to another identical collection of controls on the same form. So far,...
13
9771
by: jac | last post by:
Hae, I have a windows form with a ComboBox an other things. On that combobox I have an eventhandler on de selectedindexchanged. But somewhere in my code want to do excecute the same code that...
5
2241
by: c#2006user | last post by:
Hi everyone, i have a vb.net program ive converted most of the code but i know events dont translate well, the original vb.net code uses withevents and .handles and i can't get the events in c# to...
0
1373
by: c#2006user | last post by:
Hi everyone, i have a vb.net program ive converted most of the code but i know events dont translate well, the original vb.net code uses withevents and .handles and i can't get the events in c#...
6
2959
by: John B | last post by:
What is the reasoning for recommending that all events have the signature EventHandler(object sender, EventArgs e). Obviously I can create a custom delegate delegate void FooHandler(string foo)...
2
8490
by: markliam | last post by:
I have auto-generated some code for a button by double clicking it. By default, the code is created with a return type of void and assigned to a click event. Now, I want the function to return a...
0
7221
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
7109
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
7313
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,...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
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,...
1
5039
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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...

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.