Find ValueMember in ComboBox that is databound from an Array

Posted on 2008-11-10
Last Modified: 2013-11-27
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

Question by:si2030
    LVL 2

    Accepted Solution


     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.

    Expert Comment

    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.


    Author Comment

    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.


    Author Closing Comment

    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.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now