JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Storing Objects in a List Box or Combo Box

You've been asked to write the employee phone directory program and place a combo box with all the employee's names in the middle of a form. Now how do you connect phone numbers to the names?

In VB6 and before, each item in list boxes and combo boxes had an ItemData property, which allowed you to store text data for each item. Now, each item in a list box or combo box is itself an object, so I'll jump ahead a few chapters to Chapter 12, where we'll start creating our own classes and objects, to show how you can store additional data for each item in these controls. This example is named ComboBoxData on the CD-ROM.

To see how this works, I'll create a new class named DataItem, and each item in a combo box will be an object of this class. (To understand how to create classes, see Chapter 12.) This class will store both the name of each combo box item and some data. I'll let the New constructor store both the name and data for each item in private data members:

Public Class DataItem
    Private Data As Single
    Private Name As String

    Public Sub New(ByVal NameArgument As String, ByVal Value As Single)
        Name = NameArgument
        Data = Value
    End Sub
        

I'll also add a ToString method, overriding the Object class's ToString method, because this method will be called when the combo box needs to display the name of each item, and I'll also add a GetData method that we can use to get the internal, private data from objects:

Public Class DataItem
    Private Data As Single
    Private Name As String

    Public Sub New(ByVal NameArgument As String, ByVal Value As Single)
        Name = NameArgument
        Data = Value
    End Sub

    Overrides Function ToString() As String
        Return CStr(Name)
    End Function

    Public Function GetData() As Single
        Return Data
    End Function
End Class
Tip 

If the list box contains objects that support properties, the DisplayMember property indicates which property of the object to show. If empty, this property is empty and the object's ToString method is used.

When the form loads, we can create 20 objects of the DataItem class—item 5 will be named "Item 5" and store the internal value 5, for example—and place them in the combo box with the Items.Add method:

Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Objects(20) As DataItem

    ComboBox1.BeginUpdate()
    Dim intLoopIndex As Integer
    For intLoopIndex = 0 To 20
        Objects(intLoopIndex) = New DataItem("Item " & _
            intLoopIndex, CSng(intLoopIndex))
        ComboBox1.Items.Add(Objects(I))
    Next
    ComboBox1.Items.AddRange(Objects)
    ComboBox1.EndUpdate()
End Sub

Main Page

Using AddRange

Because we've stored the DataItem objects in an array (named Objects here), there's another way of adding these items to a combo box or list box that's worth pointing out here—you can use the AddRange method like this to add all the objects in the Objects array to the combo box at once:

Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Objects(20) As DataItem

    ComboBox1.BeginUpdate()
    Dim intLoopIndex As Integer
    For intLoopIndex = 0 To 20
        Objects(intLoopIndex) = New DataItem("Item " & _
            intLoopIndex, CSng(intLoopIndex))
    Next
    ComboBox1.Items.AddRange(Objects)
    ComboBox1.EndUpdate()
End Sub

Now when the user selects an item in the combo box, I can use the SelectedItem property to get the selected object, and that object's GetData method to get the object's stored data (note that I must cast the item to an object of the DataItem class first, using CType), which I display in a message box. Here's the full code:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Objects(20) As DataItem
        ComboBox1.BeginUpdate()
        Dim intLoopIndex As Integer
        For intLoopIndex = 0 To 20
            Objects(intLoopIndex) = New DataItem("Item " & intLoopIndex, _
            CSng(intLoopIndex))
        Next
        ComboBox1.Items.AddRange(Objects)
        ComboBox1.EndUpdate()
End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As _
        System.Object, ByVal e As System.EventArgs) Handles _
        ComboBox1.SelectedIndexChanged
        MsgBox("The data for the item you selected is: " & _
           CType(ComboBox1.SelectedItem, DataItem).GetData())
    End Sub
End Class

Public Class DataItem
    Private Data As Single
    Private Name As String
    Public Sub New(ByVal NameArgument As String, ByVal Value As Single)
        Name = NameArgument
        Data = Value
    End Sub

    Overrides Function ToString() As String
        Return CStr(Name)
    End Function

    Public Function GetData() As Single
        Return Data
    End Function
End Class

You can see this example, ComboBoxData, at work in Figure 7.15. Now we're storing objects in combo boxes, and using object methods to store and retrieve data in those objects.


Figure 7.15: Recovering data from combo box items.
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor