ListView Control (VBCCRListView)
The VBCCRListView 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 current view modeLvwViewIcon(0) - Icon viewLvwViewSmallIcon(1) - Small icon viewLvwViewList(2) - List viewLvwViewReport(3) - Report viewLvwViewTile(4) - Tile view
ColumnHeaders(LvwColumnHeaders): Collection of column headers in report viewListItems(LvwListItems): Collection of items in ListViewSelectedItem(LvwListItem): Returns currently selected itemMultiSelect(Boolean): Enables/disables multiple item selectionSorted(Boolean): Enables/disables automatic sortingGridLines(Boolean): Shows/hides gridlines in report viewFullRowSelect(Boolean): Enables/disables full row selectionLabelEdit(LvwLabelEditConstants): Label editing modeHoverSelection(Boolean): Hover selectionHotTracking(Boolean): Hot trackingArrange(LvwArrangeConstants): Item arrangement mode
Methods
Essential Methods
FindItem(Text As String, [Index As Long], [Partial As Boolean], [Wrap As Boolean]) As LvwListItem: Searches for an itemHitTest(X As Single, Y As Single, [SubItemIndex As Long]) As LvwListItem: Tests click positionHitTestInsertMark(X As Single, Y As Single, [After As Boolean]) As Long: Tests insert mark positionStartLabelEdit(): Starts label editing for the selected itemEndLabelEdit([Discard As Boolean]): Ends label editingGetVisibleCount() As Long: Gets the number of visible itemsRefresh(): Refreshes the displayDrag([Action]): Drag and drop operationOLEDrag(): OLE drag and dropSetFocus(): Sets focusZOrder([Position]): Sets Z order
LvwListItems Collection Methods
Add([Index As Long], [Key As String], [Text As String], [Icon As Variant], [SmallIcon As Variant]) As LvwListItem: Adds a new itemRemove(Index): Removes an itemClear(): Clears all itemsItem(Index) As LvwListItem: Gets an item
LvwColumnHeaders Collection Methods
Add([Index As Long], [Key As String], [Text As String], [Width As Long], [Alignment As VBRUN.AlignmentConstants], [Icon As Variant]) As LvwColumnHeader: Adds a column headerRemove(Index): Removes a column headerClear(): Clears all column headersItem(Index) As LvwColumnHeader: Gets a column header
Events
Click(): Click eventDblClick(): Double-click eventItemClick(Item As LvwListItem, Button As Integer): Fires when an item is clickedItemDblClick(Item As LvwListItem, Button As Integer): Fires when an item is double-clickedItemFocus(Item As LvwListItem): Fires when an item receives focusItemActivate(Item As LvwListItem, SubItemIndex As Long, Shift As Integer): Fires when an item is activatedItemSelect(Item As LvwListItem, Selected As Boolean): Fires when an item selection changesItemCheck(Item As LvwListItem, Checked As Boolean): Fires when an item checkbox state changesBeforeLabelEdit(ByRef Cancel As Boolean): Fires before label editing beginsAfterLabelEdit(ByRef Cancel As Boolean, NewString As String): Fires after label editing endsColumnClick(ColumnHeader As LvwColumnHeader): Fires when a column header is clickedColumnDblClick(ColumnHeader As LvwColumnHeader): Fires when a column header is double-clickedColumnBeforeResize(ColumnHeader As LvwColumnHeader, ByRef Cancel As Boolean): Fires before column resizingColumnAfterResize(ColumnHeader As LvwColumnHeader, NewWidth As Long): Fires after column resizingKeyDown(KeyCode As Integer, Shift As Integer): Key down eventKeyPress(KeyChar As Integer): Key press eventKeyUp(KeyCode As Integer, Shift As Integer): Key up eventMouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single): Mouse down eventMouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single): Mouse move eventMouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single): Mouse up event
Code Examples
Basic Usage
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
Dim item1 As LvwListItem
Set item1 = .Add(, , "John Doe")
item1.SubItems(1) = "30"
item1.SubItems(2) = "New York"
Dim item2 As LvwListItem
Set item2 = .Add(, , "Jane Smith")
item2.SubItems(1) = "25"
item2.SubItems(2) = "London"
End With
' Set view to report mode
ListView1.View = LvwViewReport
ListView1.GridLines = True
ListView1.FullRowSelect = True
End SubHandling Events
Private Sub ListView1_ItemClick(ByVal Item As LvwListItem, ByVal Button As Integer)
MsgBox "Selected: " & Item.Text
End Sub
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As LvwColumnHeader)
' Toggle sort order based on column click
ListView1.Sorted = Not ListView1.Sorted
If Not ListView1.Sorted Then
ListView1.Sorted = True
End If
End SubFinding Items
Private Sub FindItemDemo()
Dim foundItem As LvwListItem
Set foundItem = ListView1.FindItem("John Doe", , True)
If Not foundItem Is Nothing Then
foundItem.Selected = True
foundItem.EnsureVisible
End If
End SubAdding and Removing Items
Private Sub AddItemDemo()
Dim newItem As LvwListItem
Set newItem = ListView1.ListItems.Add(, , "New Item")
newItem.SubItems(1) = "Data 1"
newItem.SubItems(2) = "Data 2"
End Sub
Private Sub RemoveItemDemo(Index As Long)
ListView1.ListItems.Remove Index
End SubLabel Editing
Private Sub ListView1_BeforeLabelEdit(ByRef Cancel As Boolean)
' Prevent editing of the first item
If ListView1.SelectedItem.Index = 1 Then
Cancel = True
MsgBox "Cannot edit the first item"
End If
End Sub
Private Sub ListView1_AfterLabelEdit(ByRef Cancel As Boolean, ByVal NewString As String)
' Validate the new label
If Len(NewString) = 0 Then
Cancel = True
MsgBox "Label cannot be empty"
End If
End SubHit Testing
Private Sub ListView1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim hitItem As LvwListItem
Dim subItemIndex As Long
Set hitItem = ListView1.HitTest(X, Y, subItemIndex)
If Not hitItem Is Nothing Then
MsgBox "Clicked: " & hitItem.Text & ", SubItem: " & subItemIndex
End If
End SubGet Visible Count
Private Sub GetVisibleItems()
Dim count As Long
count = ListView1.GetVisibleCount()
MsgBox "Visible items: " & count
End SubCommon Use Cases
File Explorer Style
Private Sub SetupFileExplorer()
With ListView1
.View = LvwViewReport
.FullRowSelect = True
.GridLines = True
.HotTracking = True
.HoverSelection = True
' Add columns
With .ColumnHeaders
.Add , , "Name", 200
.Add , , "Size", 100
.Add , , "Type", 100
.Add , , "Modified", 150
End With
End With
End SubGrouped View
Private Sub SetupGroupedView()
ListView1.View = LvwViewTile
ListView1.GroupView = True
ListView1.TileViewLines = 2
' Add items with grouping (requires Windows Vista or later)
' Grouping is handled through the Groups collection
End SubMultiple Selection
Private Sub GetSelectedItems()
Dim i As Long
For i = 1 To ListView1.ListItems.Count
If ListView1.ListItems(i).Selected Then
Debug.Print ListView1.ListItems(i).Text
End If
Next i
End SubColumn Sorting
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As LvwColumnHeader)
Static lastSortColumn As Long
Static sortOrderAscending As Boolean
If lastSortColumn <> ColumnHeader.Index Then
sortOrderAscending = True
Else
sortOrderAscending = Not sortOrderAscending
End If
lastSortColumn = ColumnHeader.Index
' Note: ListView has built-in sorting
' For custom sorting, you may need to use external methods
End SubTips
- Performance: For large lists, consider using
VirtualListItemsinstead ofListItems - Column Widths: Use
AutoSizeto automatically size columns to fit their content - Icons: Set
ImageListproperties for small and large icons to display icons in different views - Checkboxes: Enable
CheckBoxesproperty to show checkboxes for each item - LabelEdit: Set
LabelEdittoLvwLabelEditManualand callStartLabelEdit()when needed