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
etc

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.

MY QUESTION:

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
        Next

        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.

Simon






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, _
                                   clients.client_no).ToArray
 
            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

si2030Asked:
Who is Participating?
 
knk53Connect With a Mentor Commented:

 Simon,
 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.
0
 
gisTimmyCommented:
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.

0
 
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.

Simon
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.