Solved

Combo Box ValueMember/Selected Value

Posted on 2010-11-09
8
516 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now