si2030
asked on
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(strFind Str)
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
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(strFind
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
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 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.