How To Change The Combobox Index To Match A Target String

24-07-2012


string temp = "item1";

combobox1.items.add("item0");
combobox1.items.add("item1");
combobox1.items.add("item2");

I need to select “Item1” from the combo box. In other words, I need to select the index that matches the string in the “temp” variable and select that index.

Solution


int targetIndex = combobox1.FindString(temp); combobox1.selectedIndex =
targetIndex;

Note:

The FindString looks for a substring. If you need to perform an exact comparison then use Combobox.FindStringExact() Both these functions has
overloads where you can pass an integer second parameter to tell if you want the search to start from a different index than the usual 0.

The Search is NOT case sensitive on either of these functions.

In earlier versions of .net framework these are called “FindString” and “FindStringExact”.

Menol
ILT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: