C# 如何在当前组合框中选择值时使另一个组合框可见?

C# 如何在当前组合框中选择值时使另一个组合框可见?,c#,winforms,combobox,windows-applications,C#,Winforms,Combobox,Windows Applications,我有一个组合框(CB1),它包含1,2,3等项,我想在从CB1中选择值3时,使另一个组合框(CB2)可见。我应该使用哪个属性。我正在开发一个基于windows的应用程序,并使用C#作为代码隐藏语言。举个例子就可以解决这个问题。 组合框格式由以下项目列表组成: var allWiegandFormat = WiegandConfigManager.RetrieveAllWiegandFormats(); var allWiegandList = new List<IW

我有一个
组合框(CB1)
,它包含
1,2,3
等项,我想在从CB1中选择值
3时,使另一个
组合框(CB2)可见。我应该使用哪个属性。我正在开发一个基于windows的应用程序,并使用C#作为代码隐藏语言。举个例子就可以解决这个问题。
组合框格式由以下项目列表组成:

var allWiegandFormat = WiegandConfigManager.RetrieveAllWiegandFormats();
            var allWiegandList = new List<IWiegand>(allWiegandFormat);

            CBFormat.Items.Add(allWiegandList[0].Id);
            CBFormat.Items.Add(allWiegandList[3].Id);
            CBFormat.Items.Add(allWiegandList[4].Id);
            CBFormat.Items.Add(allWiegandList[5].Id);

            CBProxCardMode.Items.Add(ProxCardMode.Three);
            CBProxCardMode.Items.Add(ProxCardMode.Five);
var allWiegandFormat=WiegandConfigManager.RetrieveAllWiegandFormats();
var allWiegandList=新列表(allWiegandFormat);
CBFormat.Items.Add(allWiegandList[0].Id);
CBFormat.Items.Add(allWiegandList[3].Id);
CBFormat.Items.Add(allWiegandList[4].Id);
CBFormat.Items.Add(allWiegandList[5].Id);
CBProxCardMode.Items.Add(ProxCardMode.Three);
CBProxCardMode.Items.Add(ProxCardMode.Five);

现在,当我从CBFormat组合框中选择第二项时,我想显示CBPorxCardMode的组合框。

使用
SelectionChangeCommitted
事件并订阅您的
CB1

// In form load or form initialization
cb1.SelectionChangeCommitted += ComboBoxSelectionChangeCommitted;

// Event
private void ComboBoxSelectionChangeCommitted(object sender, EventArgs e)
{
    cb2.Visible = cb1.SelectedItem != null && cb1.Text == "3";
}

首先将CB2 Visible属性设置为False,然后通过WinForms设计器为CB1上已更改的SelectedIndexChanged添加事件处理程序代码

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    ComboBox comboBox = (ComboBox) sender;
    if(comboBox.SelectedItem != null)
    {
        int id = Convert.ToInt32(comboBox.SelectedItem)
        cbo2.Visible = (id == 3)
    }
}
这是假设您在第一个组合中添加的ID看起来是一个整数值。
还请记住,即使您以编程方式更改SelectedItem,而不仅仅是在用户更改值时,也会调用SelectedIndexChanged事件。此外,如果用户再次更改远离ID==3的选择,该方法将再次设置Cbo2不可见。

尝试此操作

Private void CB1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    Combobox CB = (ComboBox) sender;
    if(CB.SelectedIndex != -1)
    {
        int x = Convert.ToInt32(CB.Text)
        if(x == 3)
        {
          CB2.Visible = True;
        }
    }
}

如果它是
Winforms
,您可以使用如下内容

    private void Form1_Load(object sender, EventArgs e)
    {
            comboBox1.Items.Add(1);
            comboBox1.Items.Add(2);
            comboBox1.Items.Add(3);
            comboBox2.Visible = false;
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem.ToString() == "3")
        {
            comboBox2.Visible = true;
        }
        else
        {
            comboBox2.Visible = false;
        }
    }

希望这能有所帮助。,

展示您的工作并告诉人们您尝试了什么。
基于Windows的应用程序
不清楚。你在使用WinForms还是WPF技术?@SonerGönül:我已经编辑了这个问题。