Reputation: 2655
I currently have a form with about 13 buttons on it.
I want to be able to perform a function when one of those buttons are clicked. But I am trying to keep from having 13 different button click events.
Is there some way for me to be able to determine when any button click event is fired, and be able to tell which button fired it?
Thanks!
Upvotes: 1
Views: 10920
Reputation: 30512
Every proper eventhandler has an Object sender parameter. Give all 13 buttons the same handler by typing the same name in the Properties window OnClick event, for instance: OnAnyButtonClicked. The created function will be:
private void OnAnyButtonClicked(object sender, ...)
{
// sender is the button that was clicked,
// find out which button is clicked
// call the corresponding function
}
Tip:
To find out which button is pressed you could use Object.ReferenceEquals A faster way to avoid if ... then ... else if ... then ... else if... is using the Tag property of each Button. Give eacht Button.Tag an enum value corresponding to the action that has to be performed and use a switch statement to find out what has to be done.
You could also assing a delegate to the button.Tag, but that is almost the same as making a different onClick event handler for every button.
Upvotes: 0
Reputation: 199
I think that in this situation using delgates would be proper. Check these links out for further information on implementation.
Upvotes: 0
Reputation: 22565
You can have different button click handlers, and name them according to button actions, also you can have single event handler for all of them, in this case parameter sender
can be cast to button and for example by it's name, finding related button.
But I offer if you have similar behavior on group of buttons map them to a single function, but if actions are different using different method is better, but in all one form with 13 button is not good, you can change them to menu, Tab, ...
Upvotes: 2
Reputation: 7449
on the click event you get a reference to the Sender - this is the clicked button so inside that you could test for the Content or Tag and act based on the value:
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show((sender as Button).Tag.ToString());
}
Upvotes: 0
Reputation: 4629
private void OnButtonClicked(object sender, EventArgs e){
Button oButton = sender as Button;
if (oButton != null){
// your logic goes here !
}
}
oButton variables is your current button. (important to check if oButton != null
when using as
operator)
Upvotes: 0
Reputation: 22188
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
(ctl as Button).Click += MyButtonHandler;
}
protected void MyButtonHandler(object sender, EventArgs e)
{
Button clickedButton = sender as Button;
//...
}
Upvotes: 1
Reputation: 268
Have one function that handles all of the click events and use the properties of the 'sender' object to identify the specific button.
Upvotes: 4
Reputation: 3951
The first parameter to the callback function is the button that was pressed.
Upvotes: 0