Classy ListBox and ComboBox's
With the removal of the ItemData property we need a new way to store our key data. You're in for a pleasant surprise.
Language: VB.NET
Level: Beginner
by Harvey
Those of you moving from Visual Basic 6.0 to VB.NET undoubtedly will need to use the ComboBox and ListBox, only to quickly
realize the .ItemData property no longer exists. I'll explain why this is actually a good thing in a moment, but it will
take a little bit of extra work on your end to get your data associated to the ComboBox and ListBox. While we're at it,
let's write a base class that will handle our data for all future projects. Just for Reference Delphi developers have been
doing it the .NET way since Delphi v1.0 and should feel right at home.
Before I explain how the ComboBox and ListBox
work under .NET, let's take a step back and remember how they work under Visual Basic 6 and why we need to associate data
with a Combo and ListBox.
A ComboBox and ListBox both show text data to a user. It's very common for the application
developer to also associate a non-visual piece of data that will be used as a "KEY" for reference to more data or a key to a
record in the database.
To set key values in Visual Basic 6, we used the .ItemData Property and the .AddNew
property.
ListBox1.AddItem("Cats")
ListBox1.ItemData(ListBox1.AddNew) = 1
ListBox1.AddItem("Dogs")
ListBox1.ItemData(ListBox1.AddNew) = 2As you can see from the code, .ItemData can only store a Long numeric
value.
Time to forget everything you know about .ItemData – it's going, going, gone!
VB.NET no longer accepts a
single numeric value for storage; now you can store an entire object into each row in your ComboBox and ListBox. This may
seem like overkill at first, but in time you will realize just what a powerful feature it is.
Here's some simple code
to add text to a ListBox in VB.NET. It's very similar to the old Visual Basic 6 way.
ListBox1.Items.Add("Dogs")You will notice while typing in the above line of code that the .Add method takes an Object
data type, but we didn't pass in an object we passed in text. Behind the scenes, .NET has converted "Dogs" into a string
object for us and string objects, as do most objects, have a ToString method. The ComboBox and ListBox will look for a
ToString method and show any text returned from that method in the controls list. We could write a class (Object) that holds
one or more KEYs of data and assign it to our ListBox with very little work on our part.
Let's design our DataItem
Class
Public Class DataItem
Private _id As String
Private _value As String
Public Sub New(ByVal id As String, ByVal value As String)
_id = id
_value = value
End Sub
Public ReadOnly Property ID() As String
Get
Return _id
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return _value
End Get
End Property
Public Overrides Function ToString() As String
Return Value
End FunctionThe only unusual piece of code here is the ToString Method. It's important to add this method and the
Overrides option, otherwise our ComboBox and ListBox won't display the text data to the user for us.
How do we assign
this new class to our ListBox example from above? We have two options – the longhand way and the shorthand way. I'm going
to show you the shorthand because it's very clean.
ListBox1.Items.Add(new DataItem("1", "Cat"))I've defined ID in our class as a string because I don't like to code
myself into a box. There have been many clients who set up their databases with keys that contain alphanumeric values or
leading zero numbers. Obviously defining ID as Integer wouldn't work in either case. Also remember that Visual Basic 6.0
only allowed for a Long Integer value for our KEY, which required a lot of extra code to store the actual key in an array or
class and use a surrogate KEY for our ListBox or ComboBox.
Retrieving our KEY from the ListBox is almost as simple as
putting it in.
Dim DI as DataItem
DI = ListBox1.Items.Item(0)
MessageBox.Show(DI.ID) You could shorthand this into one line, as well:
Dim DI As DataItem = ListBox1.Items.Item(0)
MessageBox.Show(DI.ID) So here comes the best part of all – expanding our simple class to handle anything you can throw
at it. Let's say you create a new form with a ListBox and find out you need an additional property in our DataItem class.
Do we just copy and paste and change the class name? You better not; this is, after all, .NET and with inheritance at our
disposal we can modify our DataItem and still maintain the underlying code.
Public Class DataItemXml : Inherits DataItem
Private _xml As String
Public Sub New(ByVal id As String, ByVal value As String, _
ByVal xml As String)
MyBase.New(id, value)
_xml = xml
End Sub
Public ReadOnly Property XML() As String
Get
Return _xml
End Get
End Property
End ClassNow you just need to use the DataItemXml object instead of the original DataItem.
Dim sXML as string = ""
ListBox1.Items.Add(New DataItemXml("1", "Rabbit", sXML)What started out as a simple DataItem class has expanded into a
class that's easily extendable using inheritance. As you can see, inheritance under VB.NET is very easy to do and really
works well.
It should be apparent the .NET ListBox and ComboBox are much more powerful than their predecessor and we
have just scratched the surface of what can be done. I hope this gets you thinking outside the box with VB.NET, Objects and
Controls.
|