Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何禁用单击事件处理程序中除一个按钮外的所有按钮?_C#_Winforms - Fatal编程技术网

C# 如何禁用单击事件处理程序中除一个按钮外的所有按钮?

C# 如何禁用单击事件处理程序中除一个按钮外的所有按钮?,c#,winforms,C#,Winforms,我正在写下面的代码。请有人告诉我,在开关语句中,当按下任何一个按钮时,如何禁用所有其他按钮。实际上,我希望用户在这个应用程序中只知道一次他的运气 namespace tryAppWFA { public partial class mainWindow : Form { public mainWindow() { InitializeComponent(); } private void button_clic

我正在写下面的代码。请有人告诉我,在开关语句中,当按下任何一个按钮时,如何禁用所有其他按钮。实际上,我希望用户在这个应用程序中只知道一次他的运气

  namespace tryAppWFA
   {
      public partial class mainWindow : Form
       {
    public mainWindow()
    {
        InitializeComponent();
    }

    private void button_click(object sender, EventArgs e)
    {
        Button button=(Button)sender;
       switch (button.Name) { 
            case "button1":
               textBox.Text = "Hey ! you may get a car after one year";

               break;
            case "button2":
    textBox.Text = "Hey ! you may get a big super duper House after a year";
                break;
            case "button3":
                textBox.Text = "something";
                break;
            case "button4":
                textBox.Text = "something";
                break;
            case "button5":
                textBox.Text = "somehting"
                break;
           case "exit":
                Environment.Exit(0);
                break;
           default:
            break;

        }

       }
      }
      }

您可以执行以下操作以实现此目的:
首先,在表单的加载事件中将按钮
标记
属性设置为按钮名称:

private void MainForm_Load(object sender, EventArgs e)
{
    button1.Tag = button1.Name;
    button2.Tag = button2.Name;
    btnExit.Tag = btnExit.Name;
    //and so on...

}
然后,您要做的是遍历控件并禁用那些不是单击的控件:
请注意,此处考虑的是按钮不在groupbox中,如果是这种情况,则必须遍历容器,在这种情况下,容器就是groupbox。

private void按钮5_单击(对象发送者,事件参数e)
{
foreach(此.Controls.OfType()中的按钮项,其中(c=>c.GetType()==typeof(按钮)))
{
if(item.Name==btnExit.Name)
继续;
item.Enabled=false;
}
按钮=(按钮)发送器;
开关(按钮名称)
{
案例“button1”:
Text.Text=“嘿!一年后你可能会得到一辆车”;
打破
案例“按钮2”:
textBox.Text=“嘿!一年后你可能会得到一个超级大房子”;
打破
案例“按钮3”:
Text=“某物”;
打破
案例“按钮4”:
Text=“某物”;
打破
案例“按钮5”:
textBox.Text=“something”;
打破
违约:
打破
}
}

编辑:我还从switch语句中删除了exit按钮。

假设您知道所有按钮的名称,只需编写一个方法,使用它们的名称
this.Controls.find(“contril_name”,true).FirstOrDefault()查找控件即可
和禁用按钮。您打算禁用所有按钮还是仅禁用未单击的按钮?您的回答证明非常有用。。我从这个switch语句和这个事件处理程序中删除了exit按钮。我为此创建了另一个单独的事件处理程序。但当我按下任何按钮时,退出按钮仍会禁用。我实际上想禁用除退出按钮之外的所有按钮。所以,请告诉我该怎么做。@mac请查看编辑后的答案,如果它解决了问题标记作为答案。:)(考虑到您的退出按钮名为btnExit)谢谢,它确实解决了问题。我将把这个标记为答案。再次感谢
private void button5_Click(object sender, EventArgs e)
{
    foreach (Button item in this.Controls.OfType<Control>().Where(c => c.GetType() == typeof(Button)))
    {
        if (item.Name == btnExit.Name)
            continue;
        item.Enabled = false;
    }
    Button button = (Button)sender;
    switch (button.Name)
    {
        case "button1":
            textBox.Text = "Hey ! you may get a car after one year";
            break;
        case "button2":
            textBox.Text = "Hey ! you may get a big super duper House after a year";
            break;
        case "button3":
            textBox.Text = "something";
            break;
        case "button4":
            textBox.Text = "something";
            break;
        case "button5":
            textBox.Text = "somehting";
             break;
        default:
            break;
    }
}