Phox
Phox

Reputation: 499

How to get the selected item index on a .NET ComboBox control?

I have a ComboBox 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 ComboBox item is, because integer comparison is faster than strings, right?

How can I get the index of the selected item?

Upvotes: 1

Views: 34685

Answers (2)

H. Green
H. Green

Reputation: 755

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.

Upvotes: 0

Sani Huttunen
Sani Huttunen

Reputation: 24395

ComboBox has a SelectedIndex property.

myComboBox.SelectedIndex

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

Upvotes: 1

Related Questions