I have a ComboBox with 3 hardcoded string values:
A
B
C
If I try to change the currently selected value based on SelectedValue
, SelectedItem
, or SelectedText
, neither of them change the index to the proper item.
Currently I'm doing something like:
switch (str)
{
case 'A':
comboBox.SelectedIndex = 0;
break;
case 'B':
comboBox.SelectedIndex = 1;
break;
case 'C':
comboBox.SelectedIndex = 2;
break;
}
But as you can see it's a rather weak solution and will break if items are re-ordered, edited, or appended.
Any better ways?
The Combobox widget allows you to create a dropdown list in which the list of items can be selected instantly. However, if you want to get the index of selected items in the combobox widget, then you can use the get() method. The get() method returns an integer of the selected item known as the index of the item.
Get Index Of Selected Value (ComboBoxCell) Search the cell value (Cell. Value) in the ComboBox. Items property collection to get the index of the value selected in the combo box cell.
ComboBox. SelectedIndex Property (System.
You can do this:
comboBox.SelectedIndex = comboBox.Items.IndexOf("B");
but this also works on my computer:
comboBox.SelectedItem = "B";
There must be a problem with your strings that are hardcoded in the comboBox. Check if there are some unusual characters or white (blank) characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With