如果选择了其他组合框(C#),如何禁用组合框
如果一个不同的组合框中有某种文本或值,是否仍有禁用组合框的方法。我尝试了一些方法,但似乎无法使其发挥作用 下面是一个例子如果选择了其他组合框(C#),如何禁用组合框,c#,winforms,C#,Winforms,如果一个不同的组合框中有某种文本或值,是否仍有禁用组合框的方法。我尝试了一些方法,但似乎无法使其发挥作用 下面是一个例子 你可以在这里看看 您可以处理两个组合框的事件,如果其中一个组合框具有所需的值,则禁用另一个组合框使用组合框1的SelectedValueChanged事件检查所选值。基于此禁用或启用combobox2 private void combobox1_SelectedValueChanged(object sender, Eventargs e) { if (combob
你可以在这里看看
您可以处理两个组合框的事件,如果其中一个组合框具有所需的值,则禁用另一个组合框使用组合框1的SelectedValueChanged事件检查所选值。基于此禁用或启用combobox2
private void combobox1_SelectedValueChanged(object sender, Eventargs e)
{
if (combobox1.SelectedValue == myDisableValue)
combobox2.Enabled = false;
else
combobox2.Enabled = true;
}
类似于此,只设置您想要的任何属性,或者不清除它,或者其他。(测试组合没有数据绑定)
因此,如果(combobox1==“xxx”)Combox2被锁定?或者您会建议使用单选按钮吗?如果(Combox1.SelectedValue==“xxx”){Combox2.Enabled=false;}取决于您的场景。如果始终只有一个组合框具有有效/有用的值,则使用单选按钮更有意义谢谢我使用了此选项,但我想我将添加单选按钮并使用私有的void radioButton1_CheckedChanged(object sender,EventArgs e){comboBox4.Enabled=true;comboBox5.Enabled=false;}
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;
}
}