Changing Winforms ComboBox SelectedItem Does Not Affect BindingSource

I am making a C # / WinForms application. The problem that I could not solve (for now) is that when I change the SelectedItem from the ComboBox programmatically, it changes until the ComboBox loses focus, after which it β€œresembles” its value before assigning SelectedItem. I think it takes the old value from the binding source. When selecting an item using the user interface, the base related object is updated normally, but this is not the case when I assign a new SelectedItem value programmatically.

Just for additional information: I am trying to implement "undo", which means that I save every change somewhere and when Edit β†’ Undo I change all these changes made by the user. Interestingly, other controls (TextBoxes, NumericUpDowns) work fine.

Here are the details:

I have a ComboBox that I associate with a ComboItem object as follows:

ComboBox comboBox = new ComboBox(); List<ComboItem> items = new List<ComboItem>(); ComboList comboList = Configuration.ComboList.LoadComboList(); Combo myCombo = comboList.GetCombo(control.MemberName); if (myCombo != null) { items.Add(new ComboItem(0, "", 0.0, 0.0)); for (int index = 0; index < myCombo.ComboItems.Count; index++) { items.Add(myCombo.ComboItems[index]); } } 

where Combo and ComboList are custom classes for loading data from a configuration file. Then I set the Display and Value elements, as well as the DataSource:

 comboBox.DisplayMember = "Text"; comboBox.ValueMember = "Key"; comboBox.DataSource = items; 

"Text" and "Key" are members of the ComboItem class:

 public class ComboItem { public int Key { get; set; } public string Text { get; set; } public double Coef1 { get; set; } public double Coef2 { get; set; } public void CopyValues() {...} public override bool Equals() {...} } 

Now the problem: when canceling, I check everything that is necessary so that all casting operations are safe and understandable and try to "cancel" using this code:

 Logger.Info(controls[0], op, "ExecuteUndo"); ((ComboBox)controls[0]).Focus(); ((ComboBox)controls[0]).SelectedItem = (ComboItem)op.GetOldValue(); Logger.Info(controls[0], "AFTER CHANGE"); 

Logger is only logged. The op object is taken from the undo sequence, and it gives the corresponding value using "GetOldValue ()". This code does affect the user interface, but until the control loses focus. This happens the next time you cancel, which should affect another control and thus allow this outlier to lose focus.

I'm sure this happens in the comboBox_LostFocus event, because the first thing I do in this case is Logging, and it already shows me the value that SHOULD NOT be.

+8
c # winforms binding combobox selecteditem
source share
1 answer

I think the problem you see is that the ComboBox displays a single value, but has not yet written the value to the binding source (which does not happen until you lose focus).

You can try to do something similar to write data when you select an item (provided that there is only one data binding associated with the ComboBox ):

 private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { comboBox.DataBindings[0].WriteValue(); } 

And just to make sure that you are either subscribed to this event from the designer, or manually connect it:

 public Form1() { InitializeComponent(); comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged; } 
+14
source share

All Articles