Dimitri Dimitri - 4 years ago 100
Vb.net Question

Select Dropbox index based on predefined string

I have a dropbox with a fixed number of items, defined as:

"A --- item 1"

"B --- item 2"

"C --- item 3"

From elsewhere in the program I get a string, containing the first letter of the items. e.g. B
When the form with the dropbox loads, I want to preselect the index with that letter.

I thought about using InStr, but it doesn't seem to work.

With Frm
Dim TmpStr as String = String.Empty
TmpStr = Str.InStr(0,1)

Select Case TmpStr
Case "A"
cbox.SelectedValue = 0
Case "B"
cbox.SelectedValue = 1
Case "C"
cbox.SelectedValue = 2
End Case
End With

Answer Source

To get the character you can use TmpStr = Str.Substring(0,1) to get a single character in string format or TmpStr = Str.First() which returns the first letter as a character type.

Then, as mentioned in my comment, inside your select, set the SelectedIndex property of your Combobox to the respective index.

Note: You also need to change the End Case to End Select

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download