Note, I screwed up and asked how to get DisplayMember text, I meant to put ValueMember...

I have a combo box I'm populating from a datatable. It has two columns, one for display and one for value. I can find the index of the associated item in the combobox list using the FindString method, but how do I retrieve the value member string from the return of this?

Dim selectedText as String = ""
        Dim indexitem As Integer = ComboBox1.FindString(ComboBox1.Text)
        ComboBox1.SelectedItem = indexitem 'If I type the value in the combo box, selected item is nothing
        selectedText = ComboBox1.SelectedValue.ToString 'Throws Null Reference Exception if user typed in value into combobox rather than selecting
        Debug.Print("Found {0} at index {1}, which turns out to be code: {2}", ComboBox.Text, indexitem.ToString, selectedText)

Edited by tombihn: n/a

6 Years
Discussion Span
Last Post by tombihn

I do apologize... I answered my own question. I noticed that I was trying to set the "SelectedItem". I should've been trying to set the "SelectedIndex".

If you happen to know of a more elegant solution, please feel free to reply.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.