Find ValueMember in ComboBox that is databound from an Array

Posted on 2008-11-10
Medium Priority
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
  • 2

Accepted Solution

knk53 earned 1500 total points
ID: 22922080

 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

ID: 22926520
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

ID: 22927049
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

ID: 31515016
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

850 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