如何将值从一个组合框添加到另一个组合框

时间:2014-05-19 06:42:49

标签: winforms visual-studio-2012

我有一个包含一些值列表的组合框。从列表中选择后,我需要将组合框中的其余值添加到另一个组合框中。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,此代码将帮助您

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //If you need, clear second combo box from old values before you copy
        //comboBox2.Items.Clear();

        foreach (var item in comboBox1.Items)
        {
            if (item != comboBox1.SelectedItem)
            {
                comboBox2.Items.Add(item);

                //If you need to remove item that were copied to second combo box, from the first
                //comboBox1.Items.Remove(item);
            }
        }
    }