我寫下面的代碼。請別人告訴我,在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