Combobox is connected (and this is bad)

I am making a simple WinForms application, and I ran into some strange problem.

My form:

enter image description here

It is as simple as it can be: 3 comboboxes and two buttons - OK and Cancel.

View:

private void applyOrderButton_Click(object sender, EventArgs e) { List<string> testList = new List<string>() { "A", "B", "C" }; comboBox1st.DataSource = testList; comboBox2nd.DataSource = testList; comboBox3rd.DataSource = testList; comboBox1st.SelectedIndex = 2; comboBox2nd.SelectedIndex = 1; comboBox3rd.SelectedIndex = 0; //Presenter.DoTest(); } 

What happens after the caling applyOrderButton_Click() method (this happens after clicking the Ok button), all my comboboxes change the selected position. However, each of these comboBoxes has the same index selected โ€” in this particular case, it will be โ€œAโ€.

Then I change the change comboBox selectedIndex using my cursour (for example, I select the third comboBox to display "C"), the change is performed for all three comboBoxes. What am I doing wrong?

+5
source share
2 answers

You are using something that is present in the background of WinForms called "CurrencyManager".

Its task is to synchronize the "current record" across all connecting controls that belong to the same data source.

For example, if you added a label and linked it to the same list and linked it so that it displayed a property of one of the objects, it always displayed the property value of the same object that you selected in the drop-down list.

One of the advantages of this is that you can easily add a form that edits a list of objects, linking text fields, etc. to the properties of one of the objects and adding a navigator that allows you to go to the next or previous line. You would not need to manually guarantee that all text fields are related to the correct object, CurrencyManager will do all this for you.

However, in your case, since you have linked the same data source to all three lists, CurrencyManager will ensure that all three select the same row. If you select a new line in one of the drop-down lists, CurrencyManager will go and fix all the others to refer to the same line.

You can fix this in various ways:

  • You can override the binding context for each combo box:

     comboBox2nd.BindingContext = new BindingContext(); comboBox3rd.BindingContext = new BindingContext(); 

    Note that if you are following this route, you need to do this before assigning the SelectedIndex or SelectedItem properties, otherwise the CurrencyManager will update the other two combinations before assigning new BindingContexts.

  • You can assign different data sources for each list:

     combobox2nd.DataSource = testList.ToList(); combobox3rd.DataSource = testList.ToList(); 
+10
source

Just assign the new context to other ComboBoxes as follows:

 List<string> testList = new List<string>() { "A", "B", "C" }; comboBox1st.DataSource = testList; comboBox2nd.BindingContext = new BindingContext(); comboBox2nd.DataSource = testList; comboBox3rd.BindingContext = new BindingContext(); comboBox3rd.DataSource = testList; comboBox1st.SelectedIndex = 2; comboBox2nd.SelectedIndex = 1; comboBox3rd.SelectedIndex = 0; 

The currency manager is used to synchronize synchronized data with binding (displaying data from one record). The CurrencyManager object accomplishes this by managing the set of related data provided by the data source. For each data source associated with a Windows form, the form supports at least one currency manager. Because there can be several data sources associated with a form, the BindingContext object manages all CurrencyManager objects for any particular form. More broadly, all container controls have at least one BindingContext object to manage their currency managers.

+7
source

Source: https://habr.com/ru/post/1234667/


All Articles