A friend of mine asked me how would you do this in .net?
combobox1.add("first item")
combobox1.itemdata(combobox1.newindex) = 1
combobox1.add("second item")
combobox1.itemdata(combobox1.newindex) = 2
combobox1.add("third item")
combobox1.itemdata(combobox1.newindex) = 3
The solution we came up with is as follows. First you create a new class to hold the data you require:
Public Class ValueDescriptionPair
Public Value As Object
Public Description As String
Public Sub New(ByVal NewValue As Object, ByVal NewDescription As String)
Value = NewValue
Description = NewDescription
End Sub
Public Overrides Function ToString() As String
Return Description
End Function
End Class
To add items to the list you use code like this:
Private Sub Form1_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
ComboBox1.Items.Add(New ValueDescriptionPair(1, "first item"))
ComboBox1.Items.Add(New ValueDescriptionPair(2, "second item"))
ComboBox1.Items.Add(New ValueDescriptionPair(3, "third item"))
End Sub
The next code snippet get the value stored in Value and displays in a message box:
Private Sub ComboBox1_SelectedValueChanged _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedValueChanged
Dim ItemSelected As Integer
ItemSelected = CType(ComboBox1.SelectedItem, _
ValueDescriptionPair).Value
MsgBox("Value Selected = " & ItemSelected, MsgBoxStyle.Information)
End Sub
Despite the fact that it's more work than the VB6 approach, it is actually a very powerful way of doing things. There is no limit to the number of Values or Functions you could add to your ValueDescriptionPair class.