ListSubItem Object,  ListSubItems Collection Example

The example creates twenty ListItem objects; for each object, four ListSubItem objects are also created. Before creating ListSubItem objects, however, the code creates five ColumnHeader objectsùone for the ListItem object, and four for the ListSubItem objects. To try the example, place a ListView control on a form and paste the code into the Declarations section of the code.

Option Explicit

Private Sub Form_Load()
   Dim i As Integer ' Counter
   Dim j As Integer ' Counter for ListSubItems
   Dim sngWidth As Single
   Dim si As ListSubItem
   Dim li As ListItem

   ' You can't see ColumnHeaders or ListSubitems
   ' unless the View is set to lvwReport.
   ListView1.View = lvwReport

   ' Calculate the width of a ColumnHeader object.
   sngWidth = ListView1.Width / 5

   ' Create five ColumnHeader objects.
   For i = 1 To 5
      ListView1.ColumnHeaders.Add Text:="Col " & i, Width:=sngWidth
   Next i
   
   ' Create twenty ListItem objects. For each ListItem, create four 
   ' ListSubItem objects. Set the ForeColor for each object to red.
   For i = 1 To 20
      Set li = ListView1.ListItems.Add(Text:="Item " & i)
      For j = 1 To 4 
         Set si = li.ListSubItems.Add(Text:="Subitem " & j)
         si.ForeColor = vbRed
      Next j
   Next i
End Sub