单击任何按钮的事件(C# windows 窗体)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12554314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:39:57  来源:igfitidea点击:

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

c#visual-studio-2010windows-forms-designer

提问by Kiloku

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?

我正在开发一个程序,该程序有许多按钮,单击时应该执行类似的操作,但根据单击的按钮略有不同。问题是唯一直接的方法是为每个按钮编码,这将是一项非常重复的任务。有没有一种方法可以简单地对一个块进行编程,该块可以单击任何按钮并单击哪个按钮?

采纳答案by Adam

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 Tagproperty for the disambiguation. And last but not least, you can derive your own button from Buttonand add appropriate properties. They will even appear in the properties window.

您还可以向Tag属性添加一些有用的信息以消除歧义。最后但并非最不重要的一点是,您可以从中派生出自己的按钮Button并添加适当的属性。它们甚至会出现在属性窗口中。

回答by Olivier Jacot-Descombes

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 Clickevent of the other buttons in the properties Window. Now they all trigger the same method when they are clicked.

通过双击其中一个按钮来创建按钮单击处理程序。但不是对其他按钮执行相同操作,而是转到属性窗口并切换到事件视图。现在依次选择其余按钮中的每一个,并从Click属性窗口中其他按钮的事件下拉列表中选择刚刚创建的单击处理程序。现在它们都在被点击时触发相同的方法。

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 Tagproperty of the buttons in the properties window and use it directly without having to use a switch- or if-statement.

或者,您可以Tag在属性窗口中为按钮的属性定义一个值并直接使用它,而无需使用 switch 或 if 语句。

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

您还可以直接使用 测试特定按钮sender == button1,但这在 switch 语句中不起作用。



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

创建自己的按钮Button并添加所需的属性可能会更容易。编译后,您的按钮会出现在工具箱中,您可以在属性窗口中设置您的属性。

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);
}

回答by Jason Eades

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..

或者一个switch语句..

回答by Amit Mittal

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

是的,您可以只创建一个按钮单击事件处理程序,并使用 Visual Studio 设计器将其与所有按钮连接起来。

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.

1) 通过双击任何按钮为任何一个按钮创建 btn_click 事件处理程序。2)对于所有其他按钮,右键单击任何按钮,单击属性,转到事件,在“单击”事件上,从下拉列表中选择 btn_click。

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.

如果您希望在同一事件处理程序中的不同按钮中具有不同的功能,您可以将 sender 参数向下转换为 Button 类型,然后使用其 Name 属性来区分按钮。

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")
   {
      ....
   }
}