Solved

Text and Data value in a combobox

Posted on 2008-10-26
4
466 Views
Last Modified: 2012-05-05
Hi all,

What i need is a way to store an ID with a text label in a combobox format...

Exactly the same thing as a combobox in HTML

How can I do this in my windows VB application?

Thanks,

xavier.
0
Comment
Question by:drxav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Robin
ID: 22806765
You can use the ItemData property for this. This can contain numerical values for each item.

E.g.:


    Dim l_lIdx As Long    
    For l_lIdx = 1 To 10
        Call Combo1.AddItem("Item " & CStr(l_lIdx))
        Combo1.ItemData(Combo1.NewIndex) = 100 + l_lIdx
    Next
 
Private Sub Combo1_Click()
    Debug.Print Combo1.ItemData(Combo1.ListIndex)
End Sub

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 22807662
Hi drxav;

In Windows Forms the ComboBox.ValueMember is used to hold data with the associated items in the drop down list. This member, ValueMember, needs to be a property of the bound data source. This means to use ValueMember you need to use the DataSource of the ComboBox and not just adding items to its list.

See code snippet to for example of its use.

Fernando

Public Class Form1
 
    Public Class ComboBoxItems
 
        Private _item As String
        Public Property ItemMember() As String
            Get
                Return _item
            End Get
            Set(ByVal value As String)
                _item = value
            End Set
        End Property
 
        Private _value As String
        Public Property ValueMember() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property
 
    End Class
 
    Private cbiList As New List(Of ComboBoxItems)()
 
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 
        ' The Selected item and vale was
        Dim item As String = CType(Me.ComboBox1.SelectedItem, ComboBoxItems).ItemMember
        Dim value As String = Me.ComboBox1.SelectedValue
        MessageBox.Show("Selected Item is = " & item & " and the Value Member is = " & value)
 
    End Sub
 
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim cbi As ComboBoxItems
        RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
        For idx As Integer = 0 To 10
            cbi = New ComboBoxItems()
            cbi.ItemMember = "Item Number " & idx.ToString()
            cbi.ValueMember = idx.ToString()
            cbiList.Add(cbi)
        Next
 
        ComboBox1.DataSource = cbiList
        ComboBox1.DisplayMember = "ItemMember"
        ComboBox1.ValueMember = "ValueMember"
        AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
 
    End Sub
End Class

Open in new window

0
 
LVL 4

Author Closing Comment

by:drxav
ID: 31510068
that works fantastically!!!

Thank you so much Fernando,

Xavier.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22818714
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 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