Skip to content

ListView Control (VBComCtlListView)

The VBComCtlListView control is an enhanced replacement for the standard MSComCtl ListView control. It provides improved functionality, better performance, and enhanced visual appearance.

Properties

Key Properties

  • View: Sets or returns the current view mode

    • lvwIcon (0) - Icon view
    • lvwSmallIcon (1) - Small icon view
    • lvwList (2) - List view
    • lvwReport (3) - Report view
    • lvwTile (4) - Tile view
  • ColumnHeaders: Collection of column headers in report view

  • ListItems: Collection of items in the ListView

  • SelectedItem: Returns the currently selected item

  • MultiSelect: Enables/disables multiple item selection

  • Sorted: Enables/disables automatic sorting

  • GridLines: Shows/hides gridlines in report view

  • FullRowSelect: Enables/disables full row selection

Methods

Essential Methods

  • AddItem(Text As String): Adds a new item to the ListView
  • RemoveItem(Index As Long): Removes an item at the specified index
  • FindItem(FindWhat As String, [Where As ListFindConstants]): Searches for an item
  • Arrange([Arrangement As ListArrangeConstants]): Arranges items in icon view
  • SortItems([SortOrder As ListSortOrderConstants]): Sorts the items

Events

  • ItemClick(ByVal Item As ListItem): Fires when an item is clicked
  • ColumnClick(ByVal ColumnHeader As ColumnHeader): Fires when a column header is clicked
  • BeforeLabelEdit(Cancel As Integer): Fires before item label editing begins
  • AfterLabelEdit(Cancel As Integer): Fires after item label editing ends

Code Examples

Basic Usage

vb
Private Sub Form_Load()
    ' Add column headers
    With ListView1.ColumnHeaders
        .Add , , "Name"
        .Add , , "Age"
        .Add , , "City"
    End With
    
    ' Add items with subitems
    With ListView1.ListItems
        With .Add(, , "John Doe")
            .SubItems(1) = "30"
            .SubItems(2) = "New York"
        End With
        With .Add(, , "Jane Smith")
            .SubItems(1) = "25"
            .SubItems(2) = "London"
        End With
    End With
End Sub

Handling Events

vb
Private Sub ListView1_ItemClick(ByVal Item As ListItem)
    MsgBox "Selected: " & Item.Text
End Sub

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
    ' Sort based on column click
    ListView1.SortKey = ColumnHeader.Index - 1
    ListView1.SortOrder = Abs(ListView1.SortOrder - 1)
    ListView1.Sorted = True
End Sub

Custom Sorting

vb
Private Sub SortListView(ColumnIndex As Integer)
    With ListView1
        .SortKey = ColumnIndex
        If .SortOrder = lvwAscending Then
            .SortOrder = lvwDescending
        Else
            .SortOrder = lvwAscending
        End If
        .Sorted = True
    End With
End Sub

Common Use Cases

Creating a File Explorer-like Interface

vb
Private Sub SetupFileExplorer()
    With ListView1
        .View = lvwReport
        .FullRowSelect = True
        .GridLines = True
        
        ' Add columns
        With .ColumnHeaders
            .Add , , "Name"
            .Add , , "Size"
            .Add , , "Type"
            .Add , , "Modified"
        End With
    End With
End Sub

Implementing Search Functionality

vb
Private Sub SearchListView(SearchText As String)
    Dim Item As ListItem
    
    For Each Item In ListView1.ListItems
        If InStr(1, Item.Text, SearchText, vbTextCompare) > 0 Then
            Item.Selected = True
            Item.EnsureVisible
            Exit For
        End If
    Next Item
End Sub

Best Practices

  1. Always set appropriate column widths in report view
  2. Use BeginUpdate and EndUpdate when adding multiple items
  3. Implement error handling for item operations
  4. Consider using icons to enhance visual appearance
  5. Implement proper sorting mechanisms for large datasets

Known Issues and Workarounds

  1. Memory Management
vb
' Clear items properly
Private Sub ClearListView()
    ListView1.ListItems.Clear
    Set ListView1.Icons = Nothing
    Set ListView1.SmallIcons = Nothing
End Sub
  1. Performance Optimization
vb
' Optimize for large datasets
Private Sub AddMultipleItems()
    ListView1.Visible = False
    ListView1.BeginUpdate
    
    ' Add items here
    
    ListView1.EndUpdate
    ListView1.Visible = True
End Sub

Additional Tips

  • Use Tag property to store additional item information
  • Implement custom drawing for special visual effects
  • Consider keyboard navigation support
  • Use proper error handling for all operations
  • Implement proper cleanup in Form_Unload

VB6及其LOGO版权为微软公司所有