Visual Basic Reference

ItemData Property Example

This example fills a ListBox control with employee names and fills the ItemData property array with employee numbers using the NewIndex property to keep the numbers synchronized with the sorted list. A Label control displays the name and number of an item when the user makes a selection. To try this example, paste the code into the Declarations section of a form that contains a ListBox and a Label. Set the Sorted property for the ListBox to True, and then press F5 and click the ListBox.

  Private Sub Form_Load ()
   ' Fill List1 and ItemData array with
   ' corresponding items in sorted order.
   List1.AddItem "Judy Phelps"
   List1.ItemData(List1.NewIndex) = 42310
   List1.AddItem "Chien Lieu"
   List1.ItemData(List1.NewIndex) = 52855
   List1.AddItem "Mauro Sorrento"
   List1.ItemData(List1.NewIndex) = 64932
   List1.AddItem "Cynthia Bennet"
   List1.ItemData(List1.NewIndex) = 39227
End Sub

Private Sub List1_Click ()
   ' Append the employee number and the employee name.
   Msg = List1.ItemData(List1.ListIndex) & " "
   Msg = Msg & List1.List(List1.ListIndex)
   Label1.Caption = Msg
End Sub