Event for Click in any button (C# windows forms)

51,776

Solution 1

Assign the same event handler to all buttons.

foreach (var button in Controls.OfType<Button>()) {
    button.Click += button_Click;
}

Or you can select the same event handler in the properties window switched to events (flash icon).


private static void button_Click(object sender, EventArgs eventArgs)
{
    switch (((Button)sender).Name)
    {
        // find a way to disambiguate.
    }
}

You can also add some useful information to the Tag property for the disambiguation. And last but not least, you can derive your own button from Button and add appropriate properties. They will even appear in the properties window.

Solution 2

Create a button click handler by double-clicking one of the buttons. But instead of doing the same with the other buttons, go to the properties window and switch to events view. Now select each one of the remaining buttons in turn and choose the just created click handler from the drop down list of the Click event of the other buttons in the properties Window. Now they all trigger the same method when they are clicked.

enter image description here

private void button1_Click(object sender, EventArgs e)
{
    var btn = (Button)sender;
    switch (btn.Name) {
        case "button1":
            ...
            break;
        case "button2":
            ...
            break;
        case "button3":
            ...
            break;
        default:
            break;
    }
}

Or you can define a value for the Tag property of the buttons in the properties window and use it directly without having to use a switch- or if-statement.

You can also test for specific buttons directly with sender == button1, but this does not work in a switch statement.


It might be easier to create your own button deriving from Button and to add the required properties. Once compiled, your button appears in the Toolbox and your properties can be set in the properties window.

public class MyButton : Button
{
    public int A { get; set; }
    public int B { get; set; }
}

Usage:

private void button1_Click(object sender, EventArgs e)
{
    var btn = (MyButton)sender;
    DoSomething(btn.A, btn.B);
}

Solution 3

Is there a way to program simply one block that would get the click on any button and which button was clicked?

I would just use the same click event and conditionally check the sender for which button was clicked

private void button1_Click(object sender, System.EventArgs e)
{
   if(sender == button1)
   {
      //do something different for button1
   }
   else if(sender == button2)
   {
      ....
   }
}

Or a switch statement..

Solution 4

Yes you can create just one Button Click Event Handler and hook it up with all the buttons using visual studio designer.

It is simple, just follow these steps:

1) Create btn_click event handler for any one button by double clicking on any button. 2) For all other buttons, right click on any button, click properties, go to events, on "Click" event, select btn_click from the drop-down list.

If you want different functionality in different buttons in same event handler, you can downcast the sender parameter to Button type and then use its Name property to differentiate among buttons.

Here's an example:

private void btn_Click(object sender, System.EventArgs e)
{
   Button b =(Button)sender;
   if(b.Name == "button1")
   {
      //some code
   }
   else if(b.Name == "button2")
   {
      ....
   }
}
Share:
51,776
Kiloku
Author by

Kiloku

Updated on August 02, 2022

Comments

  • Kiloku
    Kiloku almost 2 years

    I'm developing a program that has many buttons that should do a similar action when clicked, but with a small difference based on which button was clicked. The problem is that the only straightforward path is to code this for each button, which would be a very repetitive task. Is there a way to program simply one block that would get the click on any button and which button was clicked?