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.
Dim dsDataset As New DataSet
Dim drNewRow As DataRow
dsDataset.Tables.Add()
dsDataset.Tables(0).Column s.Add("The DisplayTex t")
dsDataset.Tables(0).Column s.Add("The Value")
drNewRow = dsDataset.Tables(0).NewRow
drNewRow.Item("TheDisplayT ext") = "Item #1"
drNewRow.Item("TheValue") = 1
dsDataset.Tables(0).Rows.A dd(drNewRo w)
cboComboBox.DisplayMember = "TheDisplayText"
cboComboBox.ValueMember = "TheValue"
cboComboBox.DataSource = dsDataset.Tables(0)
Dim drNewRow As DataRow
dsDataset.Tables.Add()
dsDataset.Tables(0).Column
dsDataset.Tables(0).Column
drNewRow = dsDataset.Tables(0).NewRow
drNewRow.Item("TheDisplayT
drNewRow.Item("TheValue") = 1
dsDataset.Tables(0).Rows.A
cboComboBox.DisplayMember = "TheDisplayText"
cboComboBox.ValueMember = "TheValue"
cboComboBox.DataSource = dsDataset.Tables(0)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!