简体   繁体   English

如何在当前组合框中选择值时显示另一个组合框?

[英]How to make visible another combo box on selection of a value in current combo box?

I have a combobox(CB1) and it contains items like 1,2,3 and i want to make another combobox (CB2) visible when i select the value 3 from CB1 . 我有一个combobox(CB1) ,它包含像1,2,3这样的项目,当我3 from CB1选择值3 from CB1时,我想让另一个combobox (CB2) visible Which property should i user. 我应该使用哪个属性。 I am working on a windows based application and I am using C# as the code behind language. 我正在开发基于Windows的应用程序,我使用C#作为语言背后的代码。 An example would be great to solve the problem. 一个例子就是解决问题的好方法。 The combo box CBFormat consists of a list of items as follows: 组合框CBFormat包含以下项目列表:

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

Now I want to show the Combo box of CBPorxCardMode when i select the second item from CBFormat combo box. 现在我想在CBFormat组合框中选择第二项时显示CBPorxCardMode的组合框。

Try this 尝试这个

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

Use SelectionChangeCommitted event and subscribe your CB1 to it: 使用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";
}

Start with the CB2 Visible property set to False and add a event handler code for the SelectedIndexChanged on the CB1 through the WinForms designer 从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)
    }
}

This is supposing the ID, that you are adding at the first combo, is an Integer value as it seems. 这假设您在第一个组合中添加的ID是看似的Integer值。
Also rembember that SelectedIndexChanged event will be called even if you change the SelectedItem programmatically and not just when the user changes the value. 同样,即使您以编程方式更改SelectedItem而不仅仅是在用户更改值时,也会调用SelectedIndexChanged事件。 Also, if the user change again the selection moving away from the ID==3 the method will set again the Cbo2 not visible. 此外,如果用户再次改变远离ID == 3的选择,则该方法将再次设置Cbo2不可见。

If it is Winforms You can use Something Like this 如果是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;
        }
    }

Hope this helps., 希望这可以帮助。,

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

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