What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleBUG: Watch for hidden subitems in the ListView control
KeywordsListView, subitem, hidden item, hidden subitem
CategoriesControls, Bug Alerts
Thanks to T Adam Kelly.

To display values in a ListView control using a "detail" display, you create an item for each row. You then create subitems for the items to place data in the columns after the first. However, the control only displays a column if you have defined a column header for it. If you don't create enough column headers, you may have subitems loaded that are not visible. This can be bad because you don't see all of the data. It can also cause memory problems because the subitems take up memory even though they are not visible.

When this program starts, it creates 10 items with 5 subitems each. Click the option buttons to switch between 3 and 6 columns. When the program displays 3 columns, 3 subitems are hidden.

Private Sub Form_Load()
Dim i As Integer
Dim j As Integer
Dim list_item  As ListItem

    ' Make items and subitems.
    For i = 1 To 10
        ' Make the item.
        Set list_item = ListView1.ListItems.Add()
        list_item.Text = "Item " & i

        ' Make the subitems.
        For j = 1 To 6
           list_item.ListSubItems.Add , , "Subitem " & j
        Next j
    Next i

    ' Make 3 columns.
    optColumns(0).Value = True
End Sub

Private Sub optColumns_Click(Index As Integer)
Dim col_header As ColumnHeader
Dim i As Integer
Dim num_columns As Integer

    If Index = 0 Then
        num_columns = 3
        num_columns = 6
    End If

    For i = 1 To num_columns
        Set col_header = ListView1.ColumnHeaders.Add()
        col_header.Text = "Column " & i
        col_header.Width = ListView1.Width / 6 - 30
    Next i
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.