【问题标题】:How to make visible another combo box on selection of a value in current combo box?如何在当前组合框中选择一个值时使另一个组合框可见?
【发布时间】:2013-05-22 01:57:28
【问题描述】:

我有一个combobox(CB1),它包含1,2,3 之类的项目,当我选择值3 from CB1 时,我想创建另一个combobox (CB2) visible。我应该使用哪个属性。我正在开发一个基于 Windows 的应用程序,并且我使用 C# 作为语言背后的代码。一个例子会很好地解决这个问题。 组合框 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);

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

【问题讨论】:

  • 展示你的作品并告诉人们你尝试了什么......
  • Windows based application 不清楚。您使用的是 WinForms 还是 WPF 技术?
  • @SonerGönül:我已经编辑了这个问题。

标签: c# winforms combobox windows-applications


【解决方案1】:

使用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";
}

【讨论】:

    【解决方案2】:

    首先将 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 不可见。

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

        【解决方案4】:

        如果是Winforms你可以使用Something Like this

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

        希望这会有所帮助。,

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-20
          • 1970-01-01
          相关资源
          最近更新 更多