Combo Box : ItemData

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

Hi,
How can we give a combo itemdata value and how to access it. like wise if we
need to assign any numeric value to a selected text in combo we do following
in VB 6 but how to do this in VB.NET

combo1.additem("My Name")
combo1.ItemData(combo1.newindex)=1

in VB.Net we will add value as
combo1.items.add("My Name") but how to add an ItemData in it.

regards.
atif.
 
G

Guest

Hi Atif

ComboBoxes in .NET take an object and not just a string like VB6 did so the ItemData property is no longer present

Example of basic class that can be added to a ComboBox. Note that an overridden 'ToString' method is present in the class which is what the ComboBox will use by default for an item's display text..

\\Public Class MyClas
Private mintID As Intege
Private mstrName As Strin

Public Property ID() As Intege
Ge
Return mintI
End Ge
Set(ByVal Value As Integer
mintID = Valu
End Se
End Propert

Public Property Name() As Strin
Ge
Return mstrNam
End Ge
Set(ByVal Value As String
mstrName = Valu
End Se
End Propert

Public Overrides Function ToString() As Strin
Return mstrNam
End Functio
End Clas
//

To add the objects to the combobox..

\\Dim objMyClass as MyClas

For Each objMyClass In colMyClassCollectio
ComboBox1.Items.Add(objMyClass
Nex
//

Then you can simply just reference any property of a ComboBox item's object..

\\MessageBox.Show(CType(ComboBox1.Items(0), MyClass).ID
MessageBox.Show(CType(ComboBox1.Items(0), MyClass).Name
//

HTH
Gary
 

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