C#组合框的选择依赖于另外一个组合框 [英] C# combobox options dependent on another combobox

查看:126
本文介绍了C#组合框的选择依赖于另外一个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作计划,其中一个组合框的选项是依赖于另外一个组合框的选择选项。从第一个组合框中选定的项目选择哪些选项是在第二个组合框。有谁知道如何做到这一点?



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



<$ P按钮$ p>
{
CustomerAccount aCustomerAccount =新CustomerAccount(txtAccountNumber.Text,txtCustomerName.Text,
txtCustomerAddress.Text,txtPhoneNumber.Text);
account.Add(aCustomerAccount);

cboClients.Items.Add(aCustomerAccount.GetCustomerName());
明文();
}
赶上(例外)
{
MessageBox.Show(确保每一个文本框中填入!,错误,MessageBoxButtons.OK);
}

这是第一个组合框的selectedIndex。

 私人无效cboClients_SelectedIndexChanged(对象发件人,EventArgs五)
{

CustomerAccount custAccount =帐户[cboClients.SelectedIndex]作为CustomerAccount;
如果(custAccount!= NULL)
{
txtAccountNumberTab2.Text = custAccount.GetAccountNumber();
txtCustomerNameTab2.Text = custAccount.GetCustomerName();
txtCustomerAddressTab2.Text = custAccount.GetCustomerAddress();
txtCustomerPhoneNumberTab2.Text = custAccount.GetCustomerPhoneNo();
}
}


解决方案

添加一个的SelectedIndexChanged 事件处理程序的第一个组合框。用它来清除第二个组合框的内容和填充它与相关的项目:

 公共Form1中()
{
的InitializeComponent();
的for(int i = 0;我小于10;我++){
comboBox1.Items.Add(的String.Format(项目{0},i.ToString()));
}
comboBox1.SelectedIndex = 0;
}

私人无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs五)
{
comboBox2.Items.Clear();
的for(int i = 0;我小于5;我++)
{
comboBox2.Items.Add(的String.Format(ITEM_ {0} _ {1},
comboBox1.SelectedItem,i.ToString()));
}
comboBox2.SelectedIndex = 0;
}


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 options are in the second combobox. Does anyone know how to do this?

This is the button that adds the information to the first 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);
        }

And here is the selectedIndex for the first combobox.

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

解决方案

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

这篇关于C#组合框的选择依赖于另外一个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆