all groups > vb.net upgrade > july 2005 >
You're in the

vb.net upgrade

group:

Listbox control


Listbox control Steve Enzer
7/25/2005 2:24:40 PM
vb.net upgrade:
Hi,

I'm a VB6 programmer just getting my feet wet in VB .NET and I have a
question regarding the Listbox and Combobox.

In VB6 I could set the ItemData property via code while adding items to the
control. In .NET, it appears that SelectedValue has replaced ItemData.
I've learned how to use this property when binding to a data table, but I
have found no way to assign data values via code when using the Add method
to fill the Items collection for the control.

I'd be greatful for any enlightenment.

Steve Enzer

Re: Listbox control Herfried K. Wagner [MVP]
7/25/2005 8:40:11 PM
"Steve Enzer" <enzer@frontiernet.net> schrieb:
[quoted text, click to view]

\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22
Me.ComboBox1.Items.Add(p)

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

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 can use a bound combobox:

\\\
With Me.ListBox1

' This can be an 'ArrayList' or array too, for example.
.DataSource = Database.FindPeople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
AddThis Social Bookmark Button