Buck Beasom
asked on
Combo Box ValueMember/Selected Value
With EE help I have managed to get a working Combo Box that loads sales reps using the code attached.
I want to capture the UserID (which is the ValueMember) in the combo box as a long integer so I can use it for filter a list view. But with what I have so far, my cboSalesRep.ValueMember is null. So I am missing something.
THANKS FOR ANY HELP.
I want to capture the UserID (which is the ValueMember) in the combo box as a long integer so I can use it for filter a list view. But with what I have so far, my cboSalesRep.ValueMember is null. So I am missing something.
THANKS FOR ANY HELP.
Private Sub cboSalesRep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSalesRep.SelectedIndexChanged
'Set the Public variable to the Combo Box Value
lngSelectedSalesRepID = CLng(Me.cboSalesRep.SelectedValue)
End Sub
SelectedValue will only work with DataBinding. You need to cast the SelectedItem to your custom type and retrieve the ID from it.
ASKER
The Mozz plan throws an error. VB doesn't seem to like that syntax.
Carl, you've gotten me this far (and I thought I'd give you a break!) I don't know how to code "cast the SelectedItem to your custom type". I'm WAY new at this.
Thanks.
Carl, you've gotten me this far (and I thought I'd give you a break!) I don't know how to code "cast the SelectedItem to your custom type". I'm WAY new at this.
Thanks.
Something like:
Dim item As ListItem = CType(cboSalesRep.Selected Item, ListItem)
Dim id As Integer = item.ID
Dim item As ListItem = CType(cboSalesRep.Selected
Dim id As Integer = item.ID
Does it help to add a call to ToString() at the end of your code:
lngSelectedSalesRepID = CLng(Me.cboSalesRep.Select edValue.To String)
Ref: https://www.experts-exchange.com/questions/23230044/Get-a-Value-from-Combo-Box-in-VB-NET-Winforms.html
lngSelectedSalesRepID = CLng(Me.cboSalesRep.Select
Ref: https://www.experts-exchange.com/questions/23230044/Get-a-Value-from-Combo-Box-in-VB-NET-Winforms.html
ASKER
Carl:
I put the code in the cboSalesRep_SelectedIndexC hanged event.
The comma after SelectedItem throws an error.
I put the code in the cboSalesRep_SelectedIndexC
The comma after SelectedItem throws an error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Saying "YOU DA MAN."
I tried to combine two steps into 1, so instead of using CType the way you had it, and then setting the lngSelectedRepID variable to the resulting id value, I tried using Clng.
When I did it this way:
Private Sub cboSalesRep_SelectedIndexC hanged(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles cboSalesRep.SelectedIndexC hanged
'Set the Public variable to the Combo Box Value
Dim item As ListItem = CType(cboSalesRep.Selected Item, ListItem)
Dim lngItemID As Long = item.ID
lngSelectedSalesRepID = lngItemID
End Sub
It worked like a charm!
Thanks again!!!!
I tried to combine two steps into 1, so instead of using CType the way you had it, and then setting the lngSelectedRepID variable to the resulting id value, I tried using Clng.
When I did it this way:
Private Sub cboSalesRep_SelectedIndexC
'Set the Public variable to the Combo Box Value
Dim item As ListItem = CType(cboSalesRep.Selected
Dim lngItemID As Long = item.ID
lngSelectedSalesRepID = lngItemID
End Sub
It worked like a charm!
Thanks again!!!!
lngSelectedSalesRepID = CLng(Me.cboSalesRep.Items[