VListBox Control (VBCCRVListBox)
VListBox control is a virtualized listbox control that efficiently handles large lists using virtualization technology. Compared to regular listboxes, it only keeps currently visible items in memory, significantly improving performance and reducing memory usage.
Properties
Basic Properties
VirtualItemCount
- Total number of virtual itemsVisibleItemCount
- Number of visible itemsItemHeight
- Item heightTopIndex
- Top item indexListIndex
- Currently selected item indexBackColor
- Background colorForeColor
- Foreground colorEnabled
- Whether control is enabledFont
- Font settingsVisible
- Whether visible
Appearance Properties
BorderStyle
- Border styleIntegralHeight
- Whether to use integral heightMultiSelect
- Whether to allow multiple selectionSorted
- Whether to automatically sortStyle
- Appearance stylevbListBoxStandard
(0) - Standard stylevbListBoxCheckbox
(1) - With checkboxes
Events
ItemRequest
- Triggered when requesting item dataClick
- Triggered when control is clickedDblClick
- Triggered when control is double-clickedGotFocus
- Triggered when control gains focusKeyDown
- Triggered when key is pressedKeyPress
- Triggered during key pressKeyUp
- Triggered when key is releasedLostFocus
- Triggered when control loses focusMouseDown
- Triggered when mouse button is pressedMouseMove
- Triggered when mouse movesMouseUp
- Triggered when mouse button is releasedScroll
- Triggered when list scrollsItemCheck
- Triggered when item check state changesSelectionChanged
- Triggered when selection changes
Code Examples
Basic Usage
vb
Private Sub InitVListBox()
With VListBox1
.VirtualItemCount = 1000000 ' Set total virtual item count
.VisibleItemCount = 15 ' Set visible item count
.ItemHeight = 20 ' Set item height
End With
End Sub
Private Sub VListBox1_ItemRequest(ByVal Index As Long, _
ByVal Count As Long, _
Items() As String)
' Handle item request
Dim i As Long
For i = 0 To Count - 1
Items(i) = "Item #" & (Index + i)
Next i
End Sub
Virtual Data Manager
vb
Private Type ItemData
Text As String
Value As Variant
Checked As Boolean
Tag As String
End Type
Private Type DataManager
Items() As ItemData
Count As Long
PageSize As Long
CacheEnabled As Boolean
Cache As Collection
SelectedIndices() As Long
SelectedCount As Long
End Type
Private Manager As DataManager
Private Sub InitDataManager(ByVal Count As Long, _
Optional ByVal PageSize As Long = 100)
With Manager
.Count = Count
.PageSize = PageSize
.CacheEnabled = True
Set .Cache = New Collection
ReDim .SelectedIndices(1 To 100)
.SelectedCount = 0
' Initialize data array
ReDim .Items(0 To Count - 1)
' Fill with sample data
Dim i As Long
For i = 0 To Count - 1
With .Items(i)
.Text = "Item #" & i
.Value = i
.Checked = False
.Tag = "TAG" & i
End With
Next i
End With
End Sub
Database Integration
vb
Private Sub LoadDatabaseItems()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
' Get total count
rs.Open "SELECT COUNT(*) FROM Items", cn
VListBox1.VirtualItemCount = rs(0)
rs.Close
' Configure listbox
With VListBox1
.VisibleItemCount = 20
.ItemHeight = 20
.Style = vbListBoxCheckbox
End With
End Sub
Private Sub VListBox1_ItemRequest(ByVal Index As Long, _
ByVal Count As Long, _
Items() As String)
' Load items from database
Dim SQL As String
SQL = "SELECT ItemName FROM Items " & _
"ORDER BY ItemID " & _
"OFFSET " & Index & " ROWS " & _
"FETCH NEXT " & Count & " ROWS ONLY"
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open SQL, cn
Dim i As Long
i = 0
While Not rs.EOF And i < Count
Items(i) = rs("ItemName")
rs.MoveNext
i = i + 1
Wend
rs.Close
End Sub
Common Use Cases
Multi-Select Implementation
vb
Private Sub SetupMultiSelect()
With VListBox1
.MultiSelect = True
.Style = vbListBoxCheckbox
End With
End Sub
Private Sub VListBox1_ItemCheck(ByVal Index As Long, _
ByVal Checked As Boolean)
' Handle item check/uncheck
If Checked Then
AddToSelection Index
Else
RemoveFromSelection Index
End If
End Sub
Private Sub AddToSelection(ByVal Index As Long)
With Manager
.SelectedCount = .SelectedCount + 1
If .SelectedCount > UBound(.SelectedIndices) Then
ReDim Preserve .SelectedIndices(1 To .SelectedCount * 2)
End If
.SelectedIndices(.SelectedCount) = Index
End With
End Sub
Best Practices
- Memory Management
vb
Private Sub OptimizeMemory()
' Implement data paging
Const PAGE_SIZE As Long = 100
With VListBox1
.CacheSize = PAGE_SIZE * 2 ' Cache two pages
.PrefetchItems = True ' Enable prefetching
.PrefetchCount = PAGE_SIZE ' Prefetch one page
End With
End Sub
- Performance Optimization
vb
Private Sub OptimizePerformance()
' Suspend updates during bulk operations
VListBox1.BeginUpdate
' Perform multiple operations
LoadLargeDataSet
' Resume updates
VListBox1.EndUpdate
End Sub
Known Issues and Solutions
- Scrolling Performance
vb
Private Sub HandleScrolling()
' Implement smooth scrolling
With VListBox1
.SmoothScroll = True
.ScrollDelay = 10
.ScrollLines = 3
End With
End Sub
- Selection Management
vb
Private Sub ManageSelection()
Static IsUpdating As Boolean
If IsUpdating Then Exit Sub
IsUpdating = True
' Update selection state
UpdateSelectionState
' Update UI
RefreshUI
IsUpdating = False
End Sub
Additional Tips
- Custom Item Drawing
vb
Private Sub VListBox1_DrawItem(ByVal hDC As Long, _
ByVal Index As Long, _
ByVal State As DrawItemStateConstants, _
ByVal Left As Long, _
ByVal Top As Long, _
ByVal Right As Long, _
ByVal Bottom As Long)
' Implement custom drawing
If State And ODS_SELECTED Then
' Draw selected item
DrawSelectedItem hDC, Left, Top, Right, Bottom
Else
' Draw normal item
DrawNormalItem hDC, Left, Top, Right, Bottom
End If
End Sub
- Data Filtering
vb
Private Sub ImplementFiltering()
' Set up filter
Dim Filter As String
Filter = txtSearch.Text
' Apply filter
VListBox1.BeginUpdate
VListBox1.VirtualItemCount = GetFilteredCount(Filter)
VListBox1.EndUpdate
End Sub
Private Function GetFilteredCount(ByVal Filter As String) As Long
' Count items matching filter
Dim Count As Long
Count = 0
Dim i As Long
For i = 0 To Manager.Count - 1
If InStr(1, Manager.Items(i).Text, Filter, vbTextCompare) > 0 Then
Count = Count + 1
End If
Next i
GetFilteredCount = Count
End Function
- State Management
vb
Private Sub SaveState()
' Save selection state
Dim SelState As String
SelState = GetSelectionState()
SaveSetting App.Title, "VListBox", "Selection", SelState
SaveSetting App.Title, "VListBox", "TopIndex", VListBox1.TopIndex
End Sub
Private Sub RestoreState()
' Restore selection state
Dim SelState As String
SelState = GetSetting(App.Title, "VListBox", "Selection", "")
RestoreSelectionState SelState
VListBox1.TopIndex = GetSetting(App.Title, "VListBox", "TopIndex", 0)
End Sub