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