Find ValueMember in ComboBox that is databound from an Array

Hi there,

My problem relates to a standard comboBox.

First, what I am trying to acomplish....

I have a label and then a comboBox next to each other. The comboBox displays a dropdown of a client number and the client last name.  they are concatinated eg

1000 SMITH
1001 BROWN

What I want is to search the drop down on the name ONLY, not the client number. That is Type in an "S" and immediately the combobox would display SMITH with the MITH being the highlighted area. If I leave the comboBox the selected index will use the clientNo for that row and populate the label next to the combobox.

So the Combobox has the concatenated clientNo and lastName but the combobox.text shows just the name and if i leave I get the clientNo in the label.

OK so I used a LINQ comand to pick up all the clients and create an array that has effectively THREE columns  and then add this to the combobox. THIS WORKS. eg I get a comboBox that has the correct display member (and I assume two value members). See the added code area.


I wanted to use "findString" to match the string entered into the comboBox to the VALUE MEMBER not the display member. The following only tests on the display member that has as its first 5 digits the client number (as above).

intIdx = cbxTest.FindString(strFindStr)

The fact is I want to use this on the VALUE MEMBER - "Last_name" not the display member -  "printPosLastName"... or alternatively code that does the same test.  At the moment I cannot even get the system to recognise the value member... I am using this to create an autocomplete subroutine along the lines of:

                 intIdx = cbxTest.FindString(cbxTest.text)

                If intIdx <> -1 Then ' String found in the list.

                    cbx.SelectedText = ""
                    cbx.SelectedIndex = intIdx

                    cbx.SelectionStart = strFindStr.Length
                    cbx.SelectionLength = cbx.Text.Length

I also tried this but I am guessing it will match a full row not a partial match - it doesnt work anyway:

Dim aIndex As Integer

    With cbxTest
        For aIndex = 0 To .Items.Count - 1
            If CType(.Items(aIndex)(1), String).Trim = cbxTest.Text.Trim Then
                .SelectedIndex = aIndex
                Exit For
            End If

        If aIndex >= .Items.Count Then .SelectedIndex = -1
    End With

I get the following error: "No default member found for type 'VB$AnonymousType_0(Of String,String,Integer)'." on the IF Ctype(.items.....

Hope I have explain this correctly and given enough for you to follow.


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim clientItems = (From clients In db.clients _
                                   Order By clients.last_name _
                                   Select printPosLastName = clients.client_no & " " & clients.last_name, _
                                   clients.last_name, _
            With cbxTest
                .DataSource = clientItems
                .DisplayMember = "printPosLastName"
                .ValueMember = "last_name"
                .ValueMember = "client_no"
                .SelectedIndex = 0
            End With
            cbxTest.Text = ""
        Catch ex As Exception
        End Try
    End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


 I have never actually used the DISPLAY MEMBER and VALUEMEMBER properties of the ComboBox or ListBox to accomplish this kind of functionality.  It seems that that it may not operate correctly with an array.   You might have to use an arraylist to accomplish the functionality you are trying to achieve. I would suggest that you
1. Use a KeyDown event to capture the keyboard input
2. Once you have the keyboard input, search the array manually for the correct data.  For example if the user types an "S" Search your array's last name column for the first one that begins with an "S"
3. If you find the user input, use the cbxTest.FindString to find the concatenated data in the combo box.  Then programatically set the selectedindex of the box.
Of course you may have to handle the DrawItem event to highlight only the parts of the combo box you are interested in.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I think the UI you're describing would be confusing for some users.

It sounds like you want them to type a name, and when you find a match, make that match the selected item. Wouldn't the data you have replace what they were typing with Client_no followed by client_name? If so, how would they type a second letter?

You could put the name first in the combo box to avoid that. Then you could just search your DisplayMember values.

si2030Author Commented:
Hi gisTimmy

I have used this concept before and it works really well - eg select a client for the purpose of paying a cheque. The fact is when you want to find a client you would search for them by name. Alternatively you would search for them by client number. All the display is doing is showing the row selected in the drop down as its displayed in the drop down but spread across the label and the combobox. This is part of user control with three controls on it... label, combobox and then button to actually search in a form for a client.

Thanks for your reply though.

si2030Author Commented:
The core of this was correct. I used the original datasource and looped through that. Unfortuately when you replace the text i nthe text box you loose your position in the dropdown list... it goes to the start. :( I found a multicolumn combobox that I have managed to shoehorn into my project.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.