Link to home
Start Free TrialLog in
Avatar of stephenkobrien
stephenkobrien

asked on

Using values in combox in windows application

I'm writing a windows application in VB.NET.  I'm using a combobox to display text, but I want to store a numeric value with each line.  When an item is selected, I want to be able to retrieve the numeric value.  I know how to do this in a web application but it appears to be totally different in windows apps.  This was fairly easy to do in VB6 but I can't figure out how to do it in VB.NET.  Would appreciate any help.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Hi stephenkobrien,

Is your combobox bound to a source?

You normally bind the DisplayMember (value that the user will see) and the ValueMember (your value accessible using SelectedValue).

Cheers!
Avatar of ZeonFlash
ZeonFlash

       Dim dsDataset As New DataSet
        Dim drNewRow As DataRow

        dsDataset.Tables.Add()
        dsDataset.Tables(0).Columns.Add("TheDisplayText")
        dsDataset.Tables(0).Columns.Add("TheValue")
        drNewRow = dsDataset.Tables(0).NewRow

        drNewRow.Item("TheDisplayText") = "Item #1"
        drNewRow.Item("TheValue") = 1
        dsDataset.Tables(0).Rows.Add(drNewRow)

        cboComboBox.DisplayMember = "TheDisplayText"
        cboComboBox.ValueMember = "TheValue"
        cboComboBox.DataSource = dsDataset.Tables(0)
Avatar of stephenkobrien

ASKER

Actually, I found the answer at msdn.  You need to create a class that exposes public properties that are assigned to the DisplayMember and ValueMember properties of the control.  See the link: http://msdn2.microsoft.com/en-us/library/system.windows.forms.listcontrol.valuemember.aspx
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial