Solved

Combo Box ValueMember/Selected Value

Posted on 2010-11-09
8
519 Views
Last Modified: 2012-05-10
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.
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

Open in new window

0
Comment
Question by:Buck_Beasom
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:TheMozz
ID: 34094718
Try using the Items property along with the selected index property:

lngSelectedSalesRepID = CLng(Me.cboSalesRep.Items[Me.cboSalesRep.SelectedIndex].Value)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34094989
SelectedValue will only work with DataBinding. You need to cast the SelectedItem to your custom type and retrieve the ID from it.
0
 

Author Comment

by:Buck_Beasom
ID: 34095121
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.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34095248
Something like:

      Dim item As ListItem = CType(cboSalesRep.SelectedItem, ListItem)
      Dim id As Integer = item.ID
0
 
LVL 8

Expert Comment

by:TheMozz
ID: 34095272
Does it help to add a call to ToString() at the end of your code:

lngSelectedSalesRepID = CLng(Me.cboSalesRep.SelectedValue.ToString)

Ref: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_23230044.html
0
 

Author Comment

by:Buck_Beasom
ID: 34095488
Carl:

I put the code in the cboSalesRep_SelectedIndexChanged event.

The comma after SelectedItem throws an error.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34095630
Saying what?
0
 

Author Closing Comment

by:Buck_Beasom
ID: 34095822
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_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSalesRep.SelectedIndexChanged
        'Set the Public variable to the Combo Box Value
        Dim item As ListItem = CType(cboSalesRep.SelectedItem, ListItem)
        Dim lngItemID As Long = item.ID

        lngSelectedSalesRepID = lngItemID
    End Sub

It worked like a charm!

Thanks again!!!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

806 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