当组合框中的选定值发生变化时,禁用按钮

本文关键字:变化 按钮 组合 | 更新日期: 2023-09-27 18:04:18

我有一个按钮和一个组合框。comboBox有2个值,'是和否',我想禁用按钮,如果选择的值是没有,而我想启用,如果选择的值是是的我会怎么做,我不知道我会把代码,我的代码似乎是错误的。

if (ComboBoxCustType.SelectedIndex = 0)
        {
            Button1.Enabled = false;
        }
        else
            Button1.Enabled = true;

当组合框中的选定值发生变化时,禁用按钮

因为你没有指定WinForms或WPF,所以这是针对WinForms的

你必须在ComboBox上创建一个事件。SelectedIndexChanged并在事件处理程序中编写代码来处理selecteditem文本。

public Form1()
{
  comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
  InitializeComponent();
  CheckSelction();
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  CheckSelction();
}
void CheckSelction()
{
  if (comboBox1.SelectedItem != null)
  {
    var item = comboBox1.SelectedItem.ToString();
    button1.Enabled = item == "yes";
  }
  else
    button1.Enabled = false;
}