By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,431 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

Event to multiple controls?

P: n/a
Is there a way to attach event handler code to multiple controls?
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 Smith" <ke*********@verizon.net> wrote in message
news:ZwOXd.91489$uc.65915@trnddc04...
Is there a way to attach event handler code to multiple controls?

Nov 16 '05 #2

P: n/a
Keith Smith wrote:
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
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.