Adjusting to life without ItemData
In VB6, the ListBox and ComboBox controls had the ItemData
property. This was a “companion” list of long integers which
could be used to store additional information related to an
Item. For example, you could display a list of individuals
in a ListBox and keep their age in the ItemData property:
Dim Pos As Integer
Pos = List1.AddItem("Sneezy")
List1.ItemData(Pos) = 44
Pos = List1.AddItem("Grumpy")
List1.ItemData(Pos) = 56
Pos = List1.AddItem("Dopey")
List1.ItemData(Pos) = 22
The .NET ListBox class no longer contains an ItemData property. However, it is no longer necessary since the ListBox class in .NET gives us a more power and control for maintaining the items.
All .NET objects are derived from System.Object. This gives every .NET object a base set of methods:
Equals GetHashCode GetType ToString
It’s the last one, ToString, which will help us with the
ListBox. By default, the ToString method returns the fully
qualified name of the type of the Object. However, most
classes override this property to provide something
meaningful. For example, you might have an Employee class
where the ToString method is overridden to return the
Employee’s full name:
Public Class Employee
Private m_FirstName As String
Private m_LastName As String
Public Sub New(firstName As String, lastName As String)
m_FirstName = firstName
m_LastName = lastName
End Sub
Public Overrides Function ToString() As String
Return m_LastName & ", " & m_FirstName
End Function
End Class
The designers of .NET’s ListBox class decided to use this to their advantage. When adding items to a ListBox, you're no longer restricted to only String values. In fact, the ListBox.Items.Add() method accepts an “Object” datatype. Therefore, you can add anything you want to a .NET ListBox -- a String, an Integer, an Employee object -- anything!
Here’s an example of populating a ListBox with three
Employee objects:
ListBox1.Items.Add(New Employee("Jon", "Smith"))
ListBox1.Items.Add(New Employee("Mike", "Smith"))
ListBox1.Items.Add(New Employee("Dolly", "Madison"))
You might be asking, “So what gets displayed in the ListBox if I add an Employee object”? The .NET ListBox will use the objects “ToString” method to determine the value that will be displayed in the ListBox. It still holds a reference to the entire Employee object, but the return value of the ToString method is used to display a value in the ListBox.
Next question: What if you’re already overriding ToString()
to provide different functionality and its output would not
be useful for the ListBox? No problem -- the .NET designers
thought of that. The “DisplayMember” property can be used to
tell .NET which property to use to obtain the value to be
displayed. For example, let’s change our Employee class to
provide a FullName property:
Public Class Employee
Private m_FirstName As String
Private m_LastName As String
Public Sub New(firstName As String, lastName As String)
m_FirstName = firstName
m_LastName = lastName
End Sub
Public ReadOnly Property FullName As String
Get
Return m_LastName & ", " & m_FirstName
End Get
End Property
End Class
You can use the same code shown earlier to populate the
ListBox, but now, set the DisplayMember property before
adding the items:
ListBox1.DisplayMember = "FullName"
As you can see, .NET make maintaing the items in a ListBox easier and more powerful.