How to Disable comboBox if other comboBox is selected (C#)

后端 未结 4 1525
臣服心动
臣服心动 2021-01-03 09:21

Is there anyway to disable a combobox if a different combobox has some sort of text or value in it. I have tried a couple things and can\'t seem to get it to work.

B

相关标签:
4条回答
  • 2021-01-03 10:02

    You can take a look here

    http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/04f92630-8c66-4e9d-8c95-22716d86177f

    0 讨论(0)
  • 2021-01-03 10:17

    Something similar to this, only set whatever property you want, or don't clear it, or whatever. (test combos were not data bound)

        public partial class Form1 : Form
    {
        bool fireEvents = true;
        public Form1()
        {
            InitializeComponent();
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (fireEvents) doCheck(sender, e);
        }
    
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (fireEvents) doCheck(sender, e);
        }
    
        private void doCheck(object sender, EventArgs e)
        {
            fireEvents = false; // because we don't have a way to cancel event bubbling
            if (sender == comboBox1)
            {
                comboBox2.SelectedIndex = -1;
            }
            else if (sender == comboBox2)
            {
                comboBox1.SelectedIndex = -1;
            }
            fireEvents = true;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-03 10:18

    You can handle the SelectedValueChanged event of both the combo boxes and if any of the combo has your required value disable the other one

    0 讨论(0)
  • 2021-01-03 10:21

    Use the SelectedValueChanged event of combobox1 to check for the selected values. Disable or enable combobox2 based upon that.

    private void combobox1_SelectedValueChanged(object sender, Eventargs e)
    {
        if (combobox1.SelectedValue == myDisableValue)
            combobox2.Enabled = false;
        else
            combobox2.Enabled = true;
     }
    
    0 讨论(0)
提交回复
热议问题