如何在C sharp中单击另一个combox列表数据时禁用组合框?

时间:2017-02-17 08:11:19

标签: c# visual-studio-2013

我想在 Visual Studio 2013 上使用C sharp设计一个窗体。

我从here查看来源。但没有得到适当的。

因为我有3个组合框。我想在点击combobox1 NSSCM 元素时禁用combobox2,并在点击 NSSFO 元素时启用。

以下是我的代码段:

 namespace NSE_First_Form
    {
  public partial class Form1 : Form
    {
      public Form1()
       {
        InitializeComponent();
       }

     private void Form1_Load(object sender, EventArgs e)
       {

          MaximizeBox = false;
          MinimizeBox = false;
           if (true)
              {
               comboBox1.Items.Add(Exchange.NSSCM.ToString());
               comboBox1.Items.Add(Exchange.NSSFO.ToString());
               comboBox1.Items.Add(Exchange.BSSCM.ToString());
              }
       }

    private void button1_Click(object sender, EventArgs e)
      {
          string selectedItem = string.Empty;
          ProcessValue(selectedItem);
      }

    public enum Exchange
      {
          NSSCM = 1,
          NSSFO = 2,
          BSSCM = 3

      }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == 0)
       comboBox2.Enabled = false;
    if (comboBox1.selectedIndex == 1)
       comboBox2.Enabled = true;
}

答案 1 :(得分:1)

试试这个:

//This will disable combobox2 on the click of it
    private void comboBox1_Click(object sender, EventArgs e)
            {
                comboBox2.Enabled = false;
            }

//This will enable combobox2  on the click of it
    private void comboBox1_Click(object sender, EventArgs e)
            {
                comboBox2.Enabled = true;
            }

因为你想点击它,所以使用CLICK事件而不是SelectedIndexChange事件。