11

May I ask why does both comboboxes trigger each other such that both have same values? Can't I share a single list and have 2 comboboxes with different selected text?

private void Form1_Load(object sender, EventArgs e)
    {
        BindingList<string> list = new BindingList<string>();
        list.Add("A");
        list.Add("B");
        list.Add("C");
        list.Add("D");

        bind(cbo1, list);
        bind(cbo2, list);
    }

    private void bind(ComboBox combobox, BindingList<string> list)
    {
        // commented lines are in actual code,
        // but appears unimportant in this question
        //combobox.DropDownStyle = ComboBoxStyle.DropDown;
        //combobox.AutoCompleteSource = AutoCompleteSource.ListItems;
        //combobox.AutoCompleteMode = AutoCompleteMode.Suggest;
        combobox.DataSource = list;
        //combobox.Focus();
        //combobox.Text = string.Empty;
        //combobox.SelectedText = string.Empty;
    }

UPDATE: Ok, now I found out the issue is that the DataSource is managed by some BindingContext and CurrencyManager to automatically synchronise the list. But I feel someone must know how to disable this behaviour.

I don't wish to use 2 different lists because I want to be able to modify this single list at runtime and have the changes be reflected on all ComboBoxes. Any method to achieve this would be greatly appreciated.

Jake
  • 11,273
  • 21
  • 90
  • 147

2 Answers2

8

You can "solve" it like this:

// combobox.DataSource = list;
var curr = new BindingSource(list, null);        
combobox.DataSource = curr;

There is a default BindingSource (Currencymanager) linked to each Form that was keeping the 2 cbx in sync. But I'm not sure what the exact rules are here. I'm not even sure if the above is a good idea or not.

For small lists I would just make separate copies.

H H
  • 263,252
  • 30
  • 330
  • 514
  • I can almost go bang my head against the wall. You know you asked me to set "cbo1.DataSource = list" and i had this line in the Form_Load while the bind() calls were commeted. so when I tried BindingSource inside bind(), it did not work... just to add on, for my case, it will not work with list. BindingList is required. Thanks! – Jake Feb 01 '11 at 11:31
0

You cannot use the same object as the datasource for 2 seperate combo boxes. You should have list1 and list2 defined and populate each combobox with each one. Using the same datasource means that a selection in one combobox is reflected in the other.

anothershrubery
  • 20,461
  • 14
  • 53
  • 98
  • thanks for the info, let me come up with a solution to my specific problem and i'll get back. – Jake Feb 01 '11 at 11:02
  • 1
    This does not make sense, does it? The datasource is just the list of items, it does not contain the selected item. So why should the selected item of one combobox determine the selected item of a different combobox using the same datasource for the list items...? – TheBlastOne Sep 01 '11 at 08:06
  • @TheBlastOne the datasource in this case refers to the data where the list will GET its list of dropdownitems. So it makes sense, or so I believe. The other datasource refers to a memory location where the UI will update as the user interacts with it. – Jake Sep 02 '11 at 13:21