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

VB6 and LOGO copyright of Microsoft Corporation