set selected item in combobox - vb.net

sharkyenergy picture sharkyenergy · Nov 5, 2013 · Viewed 69.4k times · Source

I am using this code to add a value to a combobox different then the one displayed: how to add value to combobox item

Lets suppose i have 3 values in my Combobox:

 item 1
 item 2
 item 3

If i chose item 2 from the dropdown the code in the link works. But if i TYPE item 2 manually it doesnt work because i think that typing it only sets the combobox1.text value and not the combobox1.selecteditem. I can type a value present in the dropdown, or one not present. If i type one that is present, then the selectedItem property should also be set to the proper value. can this be done?

Thanks

Answer

sharkyenergy picture sharkyenergy · Nov 5, 2013

solved this way:

Private Sub ComboBox1_Keyup(sender As Object, 
  e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp

      ComboBox1.SelectedIndex = ComboBox1.FindStringExact(ComboBox1.Text)

End Sub