SubItemIndex Property Example

This example adds three ColumnHeader objects to a ListView control. The code then adds several ListItem and Subitems using the SubItemIndex to associate the SubItems string with the correct ColumnHeader object. To try the example, place a ListView control on a form and paste the code into the form's Declarations section. Run the example.

   ' Make sure ListView control is in report view.
   ListView1.View = lvwReport

   ' Add three columns.
   ListView1.ColumnHeaders.Add , "Name", "Name"
   ListView1.ColumnHeaders.Add , "Address", "Address"
   ListView1.ColumnHeaders.Add , "Phone", "Phone"

   ' Add ListItem objects to the control.
   Dim itmX As ListItem
   ' Add names to column 1.
   Set itmX= ListView1.ListItems.Add(1, "Mary", "Mary")
   ' Use the SubItemIndex to associate the SubItem with the correct
   ' ColumnHeader. Use the key ("Address") to specify the right
   ' ColumnHeader.
   itmX.SubItems(ListView1.ColumnHeaders("Address").SubItemIndex) _
   = "212 Grunge Street"
   ' Use the ColumnHeader key to associate the SubItems string 
   ' with the correct ColumnHeader. 
   itmX.SubItems(ListView1.ColumnHeaders("Phone").SubItemIndex) _
   = "555-1212"

   Set itmX = ListView1.ListItems.Add(2, "Bill", "Bill")
   itmX.SubItems(ListView1.ColumnHeaders("Address").SubItemIndex) _
   = "101 Pacific Way"
   itmX.SubItems(ListView1.ColumnHeaders("Phone").SubItemIndex) _
   = "555-7879"

   Set itmX= ListView1.ListItems.Add(3, "Susan", "Susan")
   itmX.SubItems(ListView1.ColumnHeaders("Address").SubItemIndex) = _
   "800 Chicago Street"
   itmX.SubItems(ListView1.ColumnHeaders("Phone").SubItemIndex) = _
   "555-4537"

   Set itmX= ListView1.ListItems.Add(4, "Tom", "Tom")
   itmX.SubItems(ListView1.ColumnHeaders("Address").SubItemIndex) _
   = "200 Ocean City"
   itmX.SubItems(ListView1.ColumnHeaders("Phone").SubItemIndex) = _
   "555-0348"