-1

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

这是将信息添加到第一个组合框的按钮

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

这是第一个组合框的 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();
        }
    }
4

1 回答 1

6

SelectedIndexChanged第一个ComboBox. 使用它来清除第二个的内容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;
  }
于 2012-04-20T02:30:20.393 回答