C# combobox options dependent on another combobox

前端 未结 1 731
执念已碎
执念已碎 2021-01-17 06:18

I am working on a program in which a combobox\'s options are dependent on another combobox\'s selected option. The selected item from the first combobox chooses which optio

相关标签:
1条回答
  • 2021-01-17 06:38

    Add a SelectedIndexChanged event handler for the first ComboBox. Use it to clear the content of the second ComboBox and populate it with the related items:

    public Form1()
      {
        InitializeComponent();
        for(int i = 0; i < 10; i++) {
            comboBox1.Items.Add(String.Format("Item {0}", i.ToString()));
        }
        comboBox1.SelectedIndex = 0;
      }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
        comboBox2.Items.Clear();
        for (int i = 0; i < 5; i++)
        {
          comboBox2.Items.Add(String.Format("Item_{0}_{1}", 
                              comboBox1.SelectedItem, i.ToString()));
        }
        comboBox2.SelectedIndex = 0;
      }
    
    0 讨论(0)
提交回复
热议问题