【问题标题】:Sync 2 Comboxes C#同步 2 个组合框 C#
【发布时间】:2013-06-04 13:30:52
【问题描述】:

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

数据源 = "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);
        }
    }

【问题讨论】:

    标签: c# combobox datasource sync


    【解决方案1】:

    我试试你的..

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

    【讨论】:

    • 我已经尝试过该方法,但它触发了选定的索引更改事件并导致项目脱落?当您从 1 个组合框中删除时,您需要添加到另一个组合框中,从而创建一个邪恶的循环
    • 抱歉,这会造成无限循环
    • @user1641194:小错误..你可以再试一次
    猜你喜欢
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多