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 6 7635
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(o bject sender, System.EventArg s e)
{
Control ctrl = (Control) sender;
ctrl.Click -= new System.EventHan dler(this.butto n2_Click);
}
hope this helps
Fitim Skenderi
"Torben" <to****@superus ers.dk> wrote in message
news:c3******** *************** ***@posting.goo gle.com... 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
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.EventHan dler(this.butto n2_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!
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****@superus ers.dk> wrote in message
news:ec******** ******@TK2MSFTN GP12.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.EventHan dler(this.butto n2_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!
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!
Let's say that you have a common event handler delegate for more than one event. You can declare it separately (ex.):
System.EventHan dler commonHandler;
commonHandler= new System.EventHan dler(this.Commo nHandler);
Associate it:
this.button1.Cl ick += commonHandler;
this.button2.Cl ick += commonHandler;
Remove it from all the controls:
foreach (Control innerControl in this.Controls)
{
System.Reflecti on.EventInfo[] evinfo = innerControl.Ge tType().GetEven ts();
for (int index=0; index<evinfo.Le ngth; index++)
{
evinfo[index].RemoveEventHan dler(innerContr ol,commonHandle r);
}
}
--
Horatiu Ripa
"Torben" <to****@superus ers.dk> wrote in message news:c3******** *************** ***@posting.goo gle.com... 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
Let's say that you have a common event handler delegate for more than one event. You can declare it separately (ex.):
System.EventHan dler commonHandler;
commonHandler= new System.EventHan dler(this.Commo nHandler);
Associate it:
this.button1.Cl ick += commonHandler;
this.button2.Cl ick += commonHandler;
Remove it from all the controls:
foreach (Control innerControl in this.Controls)
{
System.Reflecti on.EventInfo[] evinfo = innerControl.Ge tType().GetEven ts();
for (int index=0; index<evinfo.Le ngth; index++)
{
evinfo[index].RemoveEventHan dler(innerContr ol,commonHandle r);
}
}
--
Horatiu Ripa
"Torben" <to****@superus ers.dk> wrote in message news:c3******** *************** ***@posting.goo gle.com... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 class derived from EventArgs ( i.e.
MyEventArgs) and when the BeginInvoke is called and the UpdateTextBox
methode is call on the UI thread the...
|
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
|
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 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...
|
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 handler more than once.
Documentation refers to a Delegate invocationlist...but I can't seem to
access it from C#.
I am looking for a way to ensure...
|
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, thanks to another programmer, I've got this working out quite nicely
for the properties:
Type ctrlType = subject.GetType();
ConstructorInfo cInfo =...
| |
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 is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexchanged on the form). So, I...
|
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 work 100%
they seem to hook up ok but when the eventhandler is called from the
method its null and fails.
or maybe im reading it wrong?
anyway...
|
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 work 100%
they seem to hook up ok but when the eventhandler is called from the
method its null and fails.
or maybe im reading it wrong?
|
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)
and event FooHandler FooHappened
What are the benefits of creating derived eventargs classes for each
event instead of creating the delegate which...
|
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
DialogResult instead, so I go and replace 'void' with DialogResult in
the function header, but then I get an error stating it's the wrong
return...
|
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...
| |
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...
|
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |