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 7653
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 parameter e (EventArgs) does not
contain the derived MyEventArgs object but a EventArgs object.
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 make that deattachment operation general? could the function
find out what event it is attaced?...
|
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 that the contents of the eventhandler list
is "well known".
|
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 = ctrlType.GetConstructor(Type.EmptyTypes);
Control retControl = (Control)cInfo.Invoke(null);
| |
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 don't have to call that code twice.
|
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 its quite a bit of code so i wont post but if any would help i
am willing to pay if thats ok on...
|
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 is much easier IMO.
Am I missing something?
|
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 type.
Double clicking the error in Visual Studio takes me to where the
EventHandler is assigned to...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |