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 modelvwIcon
(0) - Icon viewlvwSmallIcon
(1) - Small icon viewlvwList
(2) - List viewlvwReport
(3) - Report viewlvwTile
(4) - Tile view
ColumnHeaders
: Collection of column headers in report viewListItems
: Collection of items in the ListViewSelectedItem
: Returns the currently selected itemMultiSelect
: Enables/disables multiple item selectionSorted
: Enables/disables automatic sortingGridLines
: Shows/hides gridlines in report viewFullRowSelect
: Enables/disables full row selection
Methods
Essential Methods
AddItem(Text As String)
: Adds a new item to the ListViewRemoveItem(Index As Long)
: Removes an item at the specified indexFindItem(FindWhat As String, [Where As ListFindConstants])
: Searches for an itemArrange([Arrangement As ListArrangeConstants])
: Arranges items in icon viewSortItems([SortOrder As ListSortOrderConstants])
: Sorts the items
Events
ItemClick(ByVal Item As ListItem)
: Fires when an item is clickedColumnClick(ByVal ColumnHeader As ColumnHeader)
: Fires when a column header is clickedBeforeLabelEdit(Cancel As Integer)
: Fires before item label editing beginsAfterLabelEdit(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