简体   繁体   中英

How to disable a combobox on clicking on another combox list data in C sharp?

I want to design a windows form using C sharp on Visual Studio 2013 .

I go through the Source from here . but did not got it properly.

for that I have 3 combobox. I want to disable combobox2 when I click on combobox1 NSSCM element and enable when click on NSSFO element.

Below is my part of code snippet:

 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)
    {

    }
}

Try this:

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

Try this:

//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;
            }

Because you want it on click, use the CLICK event, instead of SelectedIndexChange event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM