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 the event view. Now, in turn, select each of the remaining buttons and select 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 start the same method when pressed.

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 for buttons in the properties window and use it directly, without using a switch or if statement.
You can also test specific buttons directly with sender == button1 , but this does not work in the switch statement.
It may be easier to create your own button derived from Button and add the necessary properties. After compilation, your button appears in the toolbar, and your properties can be set in the properties window.
public class MyButton : Button { public int A { get; set; } public int B { get; set; } }
Using:
private void button1_Click(object sender, EventArgs e) { var btn = (MyButton)sender; DoSomething(btn.A, btn.B); }
source share