-2

嗨,我正在努力想出一种有效的方法来同步 2 个不使用 c# 的组合框。2 个组合框包含相同的值减去任一组合框中当前选定的值。

IE

数据源 = “A” “B” “C” “D”

combobx 1 当前选择的项目 = “A” combobx 2 当前选择的项目 = “B”

需要的可用值

组合框 1 = “A” “C” “D” 组合框 2 = “B” “C” “D”

实现这一目标的最佳方法是什么?在编程 windows 窗体时,我有点新手

这是我尝试过的

是的,不快乐......这就是我尝试过的

    static List<string> ds = new List<string> { "a", "b", "c", "d" };

    BindingList<string> b1 = new BindingList<string>(ds);
    BindingList<string> b2 = new BindingList<string>(ds);

    public Form1()
    {
        InitializeComponent();

        comboBox1.DataSource = b1;
        comboBox2.DataSource = b2;
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (b2.Remove((string) comboBox1.SelectedItem))
        {
            b2.Remove((string) comboBox1.SelectedItem);
        }
        if (!b1.Contains((string) comboBox2.SelectedItem))
        {
            b1.Add((string) comboBox2.SelectedItem);
        }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (b1.Remove((string)comboBox2.SelectedItem))
        {
            b1.Remove((string)comboBox2.SelectedItem);
        }
        if (!b2.Contains((string)comboBox1.SelectedItem))
        {
            b2.Add((string)comboBox1.SelectedItem);
        }
    }
4

1 回答 1

0

我试试你的..

  public Form1()
    {
        InitializeComponent();

        comboBox1.DataSource = b1;
        comboBox2.DataSource = b2;

        Boolean lDoing1;
        Boolean lDoing2;
    }

   private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lDoing1)
        {
         Return;
        }
        if (b2.Remove((string) comboBox1.SelectedItem))
        {
            lDoing2 = True ; 
            b2.Remove((string) comboBox1.SelectedItem);
            lDoing2 = False ;
        }
        if (!b1.Contains((string) comboBox2.SelectedItem))
        {
            b1.Add((string) comboBox2.SelectedItem);
        }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lDoing2)
        {
         Return;
        }
        if (b1.Remove((string)comboBox2.SelectedItem))
        {
            lDoing1 = True ;
            b1.Remove((string)comboBox2.SelectedItem);
            lDoing1 = False ;
        }
        if (!b2.Contains((string)comboBox1.SelectedItem))
        {
            b2.Add((string)comboBox1.SelectedItem);
        }
    }
于 2013-06-08T09:31:53.730 回答