我写下面的代码。请别人告诉我,在switch语句中,当我按下其中任何一个按钮时,我怎样才能禁用所有其他按钮。我实际上只希望用户在此应用程序中了解他的运气。如何禁用单击事件处理程序中的除按钮之外的所有按钮?
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;
}
}
}
}
假设你知道所有的按钮名称,只需编写一个方法来使用它们的名字找到控件this.Controls.Find(“contril_name”,true).FirstOrDefault()'并禁用它。 – Developer
您计划禁用所有按钮还是仅禁用未点击的按钮? – Niklas