How do you add items to a listbox in vbnet in code, using a datareader, so
that one property of the listbox displays the string value that you want to
show but there is also stored in another property that which corresponds to
what was the itemdata in vb6 (the primary key of a sql table, long integer
type) that and that can be retrieved? It used to be
Mylistbox.itemdata(MyListBox(selecteditem)
I guess that was too simple ;-)
Thanks for any help,
Bob
Now you are not longer limited to two values (Text+ItemData) per Item. You
can add any object you want. The return values of the objects' ToString
method (every object has a ToString method) is the text displayed in the
listbox. Example:
public class MyItem
public readonly Text as string
public readonly ID as integer
public sub New(Text as string, ID as integer)
me.text = text
me.id = id
end sub
public overrides Function ToString() as string
return me.text
end function
end Class
'...
mylistbox.items.add(new myitem(DR("text"), DR("id")))
'...
To access an item:
dim Item as myitem
item = directcast(mylistbox.items(0), myitem)
msgbox item.text & " " & item.id
--
Armin
\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22
Me.ComboBox1.Items.Add(p)
MessageBox.Show( _
DirectCast(Me.ComboBox1.Items.Item(0), Person).ToString() _
)
.
.
.
Public Class Person
Private m_strName As Object
Private m_intAge As Integer
Public Property Name() As String
Get
Return m_strName
End Get
Set(ByVal Value As String)
m_strName = Value
End Set
End Property
Public Property Age() As Integer
Get
Return m_intAge
End Get
Set(ByVal Value As Integer)
m_intAge = Value
End Set
End Property
Public Overrides Function ToString() As String
Return m_strName & " (" & m_intAge.ToString() & ")"
End Function
End Class
///
--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>