views:

332

answers:

3

I have a combo box setup with 4 items, with indexes ranging from 0 to 3.

Later in my code, I need to do a certain event depending on what is selected. To do this I thought about comparing what the index of the selected combo box item is, because integer comparison is faster than strings, right?

Can anyone let me know how I can grab the index of the selected value? Cheers. :)

A: 

ComboBox has a SelectedIndex property.

myComboBox.SelectedIndex

Regarding comparison:
If you're not doing millions of comparisons then this "optimization" wouldn't help you.

Sani Huttunen
A: 

Are you sure that integer comparison is always faster than string comparison?

Depends how long the strings your comparing are... If you compare two strings that each only have one character then its a simple byte-wise AND operation which may be faster than comparing a 4 byte integer value.

Harley Green
Each string is 32 bytes long exactly.
Phox
A: 

Usually, you can get the list index of the current selected item using the ComboBox.SelectedIndex property.

However, I've come across situations where some text was typed into the combobox's text field, and the SelectedIndex property was not updated properly and contained the value -1 instead. In such cases, you can use the ComboBox.FindStringExact method to find the list index of the entered text:

Dim selectedIndex As Integer = myComboBox.FindStringExact(myComboBox.Text)

(Btw., if no list item is found with the specified text, that function will return -1.)

stakx