Event to multiple controls?

  • Thread starter Thread starter Keith Smith
  • Start date Start date
In the event view of the properties window, just select the event handler
method for each control, or duplicate the line of code that adds the event
method for the first control, substituting each additional control.

DalePres
 
Keith said:
Is there a way to attach event handler code to multiple controls?


Say, you have a common event handler, which you want to fire for different controls, say all buttons
should fire it among with their own events. You do this:

void MyCommonHandler(object sender, EventHandler e)
{
... do whatever ..
... usually check who is the sender ...
... like this ...
if (sender is btnFirst)
{
... do something useful ...
}
else if ( ... ) { ... }
}


Now, somewhere, say in form constructor, you say:


btnFirst.Click += new EventHandler(MyCommonHandler);
btnSecond.Click += new EventHandler(MyCommonHandler);

.... etc...

Note, that the Button.Click can have more than one event attached to it, so for example you can also
add specific event handlers to those buttons along with the common one.

Hope it helps,
Andrey
 
Back
Top