I am trying to set the value of the comboBox1.SelectedIndex to the index of an item that exists in the comboBox1.Items collection.
int selectedIndex = comboBox1.FindStringExact(stringValue);
The resulting value of selectedIndex is 0 (>= 0), which means that the stringValue was found in the comboBox1.Items collection.
Notwithstanding, the comboBox1.SelectedIndex remains -1.
CodePudding user response:
I believe the expression you're looking for should be:
comboBox1.SelectedIndex = comboBox1.FindStringExact(stringValue);`
Assigning int selectedIndex
as shown in your post doesn't actually do anything other than set a local variable named selectedIndex
to 0
. In other words, it's correct the way you have in the title of your question but incorrect in the body. Make sure you're using the right one when you go to make the assignment in your code.
CodePudding user response:
As I suspected, there was an events race. I handled both comboBox1_SelectionChangeCommitted and also comboBox1_SelectedIndexChanged. Assigning the value to comboBox1.SelectedIndex in comboBox1_SelectionChangeCommitted raised the comboBox1_SelectedIndexChanged event which altered it again. The statements are all correct. The inner logic of my application was wrong.