C#combobox选项依赖于另一个combobox

我正在开发一个程序,其中combobox的选项依赖于另一个combobox的选定选项。 第一个combobox中的所选项目选择第二个combobox中的哪些选项。 有谁知道如何做到这一点?

这是将信息添加到第一个combobox的按钮

try { CustomerAccount aCustomerAccount = new CustomerAccount(txtAccountNumber.Text, txtCustomerName.Text, txtCustomerAddress.Text, txtPhoneNumber.Text); account.Add(aCustomerAccount); cboClients.Items.Add(aCustomerAccount.GetCustomerName()); ClearText(); } catch (Exception) { MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK); } 

这是第一个combobox的selectedIndex。

  private void cboClients_SelectedIndexChanged(object sender, EventArgs e) { CustomerAccount custAccount = account[cboClients.SelectedIndex] as CustomerAccount; if (custAccount != null) { txtAccountNumberTab2.Text = custAccount.GetAccountNumber(); txtCustomerNameTab2.Text = custAccount.GetCustomerName(); txtCustomerAddressTab2.Text = custAccount.GetCustomerAddress(); txtCustomerPhoneNumberTab2.Text = custAccount.GetCustomerPhoneNo(); } } 

为第一个ComboBox添加SelectedIndexChanged事件处理程序。 用它来清除第二个ComboBox的内容并用相关项填充它:

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