Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check index of combobox based on string?

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?

like image 878
Ryan Peschel Avatar asked Nov 19 '12 18:11

Ryan Peschel


People also ask

How do I find the index of a combo box?

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.

How do I get the index of a selected item in a ComboBox in VB net?

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.

Which property of ComboBox hold the index values?

ComboBox. SelectedIndex Property (System.


1 Answers

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.

like image 196
Nikola Davidovic Avatar answered Sep 27 '22 19:09

Nikola Davidovic