简体   繁体   English

如果选择了其他comboBox,如何禁用comboBox(C#)

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

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. 我尝试了几件事,但似乎无法使其正常工作。

Below is Example 以下是示例

组合框

Use the SelectedValueChanged event of combobox1 to check for the selected values. 使用combobox1的SelectedValueChanged事件检查选定的值。 Disable or enable combobox2 based upon that. 基于此禁用或启用combobox2。

private void combobox1_SelectedValueChanged(object sender, Eventargs e)
{
    if (combobox1.SelectedValue == myDisableValue)
        combobox2.Enabled = false;
    else
        combobox2.Enabled = true;
 }

您可以处理两个组合框的SelectedValueChanged事件,如果任何一个组合具有所需的值,请禁用另一个

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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