I have a ComboBox
in my WinForms application that has the DropDown
style, or in other words it allows a user to type a value, or select it from a drop-down list. It looks as such:
My goal is to select an item from the drop-down list based on its index. So I do this:
//I know for sure that i >= 0 && i < comboBox.Items.Count
comboBox.SelectedIndex = i; //i = index of the existing item
This works just fine, unless some time after the line above I display a message:
MessageBox.Show("Error");
In this case the item in the combo box got selected roughly in 80% of the time during my tests on this computer. So there's clearly some timing issue here.
What I need to know is what is the "sure" way of selecting a ComboBox item? (And by that I mean, being sure that it is selected when the next line of code following it is executed, or do it synchronously
.)
PS. I know that I can simply assign text to its Text
property, but that's not what I'm asking for. You see in my ComboBox implementation, I may assign custom objects to each item, plus I rely on many selection-based events, such as SelectedIndexChanged
.
PS2. I'm not sure if this affects only combo boxes with the DropDown
style, or all of them.