Combobox ItemData ?

F

fniles

In VB6 in combobox you can use ItemData to assign a key to the combo box
item.
Like so:
With cboGroup
.AddItem "Database"
.ItemData(.newindex) = c_RefData
:

I understand in VB.NET there is no equivalent for ItemData. So, if I need to
assign a key to the combobox item, how can I do that ?
Thank you.
 
T

Tom Shelton

In VB6 in combobox you can use ItemData to assign a key to the combo box
item.
Like so:
With cboGroup
.AddItem "Database"
.ItemData(.newindex) = c_RefData
:

I understand in VB.NET there is no equivalent for ItemData. So, if I need to
assign a key to the combobox item, how can I do that ?
Thank you.

The VB.NET combobox is not limited to a strings with a Long value for
the .ItemData. You can store whole objects in the .net combobox:

public class TheDataItem
...
public property Name() as string
...
end property
public property Id () as integer
...
end property
...
end class

dim dataItem as new TheDataItem()
dataitem.name = "john"
dataitem.id = 123
combobox.displaymemeber = "Name"
combobox.items.add(dataitem)

HTH,
Tom Shelton
 
H

Herfried K. Wagner [MVP]

fniles said:
I understand in VB.NET there is no equivalent for ItemData. So, if I need
to assign a key to the combobox item, how can I do that ?

\\\
Me.ComboBox1.Items.Add(New Person("Pink Panther", 22)

' Test.
MsgBox(DirectCast(Me.ComboBox1­.Items(0), Person).ToString())
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer

Public Sub New(ByVal Name As String, ByVal Age As Integer)
Me.Name = Name
Me.Age = Age
End Sub

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///

Alternatively you could use the control's 'DataSource', 'DisplayMember', and
'ValueMember' properties, for example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top