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

I am developing a program in which there are many buttons that should do a similar action when pressed, but with a slight difference, on the basis of which the button was pressed. The problem is that the only easy way is the code for each button, which will be a very repetitive task. Is there a way to program only one block that would press any button and which button would press?

+4
source share
4 answers

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, switchable 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 value. And last but not least, you can get your own button from Button and add the appropriate properties. They will even appear in the properties window.

+12
source

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.

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 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); } 
+6
source

Is there a way to program only one block that clicks on any button and which button is pressed?

I would just use the same click event and conditionally check the sender for which the 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 ..

+3
source

Yes, you can create only one Click Button event handler and connect it with all the buttons using the visual designer of the studio.

It's simple, just follow these steps:

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

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

Here is 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") { .... } } 
+1
source

Source: https://habr.com/ru/post/1435781/


All Articles