ListBoxW Control
Wraps the Win32 native list box control with support for checkbox/radio styles, owner-draw, insertion marks, and multi-column display.
Enumerations
LstStyleConstants
| Constant | Value | Description |
|---|---|---|
| LstStyleStandard | 0 | Standard list box |
| LstStyleCheckbox | 1 | Checkbox style |
| LstStyleOption | 2 | Option button style |
LstDrawModeConstants
| Constant | Value | Description |
|---|---|---|
| LstDrawModeNormal | 0 | System-drawn |
| LstDrawModeOwnerDrawFixed | 1 | Owner-draw fixed height |
| LstDrawModeOwnerDrawVariable | 2 | Owner-draw variable height |
CCBorderStyleConstants
See common enumerations.
CCMousePointerConstants
See common enumerations.
CCRightToLeftModeConstants
See common enumerations.
Properties
Text
Property Get Text() As String
Property Let Text(ByVal Value As String)Text of the currently selected item.
List
Property Get List(ByVal Index As Long) As String
Property Let List(ByVal Index As Long, ByVal Value As String)Gets or sets list item text by index.
ItemData
Property Get ItemData(ByVal Index As Long) As LongPtr
Property Let ItemData(ByVal Index As Long, ByVal Value As LongPtr)Gets or sets item-associated data by index.
ItemChecked
Property Get ItemChecked(ByVal Index As Long) As Boolean
Property Let ItemChecked(ByVal Index As Long, ByVal Value As Boolean)Gets or sets the checked state of an item by index (effective when Style is Checkbox or Option).
ListCount
Property Get ListCount() As LongTotal number of list items. Read-only.
ListIndex
Property Get ListIndex() As Long
Property Let ListIndex(ByVal Value As Long)Index of the currently selected item.
NewIndex
Property Get NewIndex() As LongIndex of the most recently added item. Read-only.
TopIndex
Property Get TopIndex() As Long
Property Let TopIndex(ByVal Value As Long)Index of the first visible item in the list.
AnchorIndex
Property Get AnchorIndex() As Long
Property Let AnchorIndex(ByVal Value As Long)Index of the selection anchor.
SelCount
Property Get SelCount() As LongNumber of selected items. Read-only.
Selected
Property Get Selected(ByVal Index As Long) As Boolean
Property Let Selected(ByVal Index As Long, ByVal Value As Boolean)Gets or sets the selected state of an item by index.
ItemHeight
Property Get ItemHeight(Optional ByVal Index As Long) As Single
Property Let ItemHeight(Optional ByVal Index As Long, ByVal Value As Single)Item height. In variable-height owner-draw mode, it can be set per index.
InsertMark
Property Get InsertMark(Optional ByRef After As Boolean) As Long
Property Let InsertMark(Optional ByRef After As Boolean, ByVal Value As Long)Index of the insertion mark.
OptionIndex
Property Get OptionIndex() As Long
Property Let OptionIndex(ByVal Value As Long)Index of the selected item in option button style.
OLEDraggedItem
Property Get OLEDraggedItem() As LongIndex of the dragged item in an OLE drag-drop operation. Read-only.
Style
Property Get Style() As LstStyleConstants
Property Let Style(ByVal Value As LstStyleConstants)List box style. Read-only at design time.
DrawMode
Property Get DrawMode() As LstDrawModeConstants
Property Let DrawMode(ByVal Value As LstDrawModeConstants)Drawing mode. Read-only at design time.
MultiSelect
Property Get MultiSelect() As VBRUN.MultiSelectConstants
Property Let MultiSelect(ByVal Value As VBRUN.MultiSelectConstants)Multi-selection mode.
Sorted
Property Get Sorted() As Boolean
Property Let Sorted(ByVal Value As Boolean)Whether to automatically sort items.
MultiColumn
Property Get MultiColumn() As Boolean
Property Let MultiColumn(ByVal Value As Boolean)Whether to enable multi-column display.
IntegralHeight
Property Get IntegralHeight() As Boolean
Property Let IntegralHeight(ByVal Value As Boolean)Whether to show only complete items. Can be set at design time.
AllowSelection
Property Get AllowSelection() As Boolean
Property Let AllowSelection(ByVal Value As Boolean)Whether to allow item selection.
UseTabStops
Property Get UseTabStops() As Boolean
Property Let UseTabStops(ByVal Value As Boolean)Whether to recognize and expand tab characters.
DisableNoScroll
Property Get DisableNoScroll() As Boolean
Property Let DisableNoScroll(ByVal Value As Boolean)Whether to disable (instead of hide) the scroll bar when scrolling is not needed.
HorizontalExtent
Property Get HorizontalExtent() As Single
Property Let HorizontalExtent(ByVal Value As Single)Horizontal scroll width.
InsertMarkColor
Property Get InsertMarkColor() As OLE_COLOR
Property Let InsertMarkColor(ByVal Value As OLE_COLOR)Color of the insertion mark.
ScrollTrack
Property Get ScrollTrack() As Boolean
Property Let ScrollTrack(ByVal Value As Boolean)Whether to scroll content in real time while dragging the scroll bar.
Redraw
Property Get Redraw() As Boolean
Property Let Redraw(ByVal Value As Boolean)Whether to redraw the list box when items change. Disabling can speed up batch additions.
BorderStyle
Property Get BorderStyle() As CCBorderStyleConstants
Property Let BorderStyle(ByVal Value As CCBorderStyleConstants)Border style. See common enumerations.
VisualStyles
Property Get VisualStyles() As Boolean
Property Let VisualStyles(ByVal Value As Boolean)Whether to enable visual styles.
BackColor
Property Get BackColor() As OLE_COLOR
Property Let BackColor(ByVal Value As OLE_COLOR)Background color.
ForeColor
Property Get ForeColor() As OLE_COLOR
Property Let ForeColor(ByVal Value As OLE_COLOR)Foreground color.
Font
Property Get Font() As StdFont
Property Let Font(ByVal NewFont As StdFont)
Property Set Font(ByVal NewFont As StdFont)Font.
Enabled
Property Get Enabled() As Boolean
Property Let Enabled(ByVal Value As Boolean)Whether the control is enabled.
AllowDropFiles
Property Get AllowDropFiles() As Boolean
Property Let AllowDropFiles(ByVal Value As Boolean)Whether to allow file drop.
OLEDragMode
Property Get OLEDragMode() As VBRUN.OLEDragConstants
Property Let OLEDragMode(ByVal Value As VBRUN.OLEDragConstants)OLE drag mode.
OLEDragDropScroll
Property Get OLEDragDropScroll() As Boolean
Property Let OLEDragDropScroll(ByVal Value As Boolean)Whether to auto-scroll during OLE drag-drop.
OLEDropMode
Property Get OLEDropMode() As OLEDropModeConstants
Property Let OLEDropMode(ByVal Value As OLEDropModeConstants)OLE drop mode.
MousePointer
Property Get MousePointer() As CCMousePointerConstants
Property Let MousePointer(ByVal Value As CCMousePointerConstants)Mouse pointer style. See common enumerations.
MouseIcon
Property Get MouseIcon() As IPictureDisp
Property Let MouseIcon(ByVal Value As IPictureDisp)
Property Set MouseIcon(ByVal Value As IPictureDisp)Custom mouse icon.
MouseTrack
Property Get MouseTrack() As Boolean
Property Let MouseTrack(ByVal Value As Boolean)Whether to enable mouse enter/leave tracking.
RightToLeft
Property Get RightToLeft() As Boolean
Property Let RightToLeft(ByVal Value As Boolean)Right-to-left display direction.
RightToLeftMode
Property Get RightToLeftMode() As CCRightToLeftModeConstants
Property Let RightToLeftMode(ByVal Value As CCRightToLeftModeConstants)Right-to-left mode. See common enumerations.
hWnd
Property Get hWnd() As LongPtrWindow handle of the list box control.
hWndUserControl
Property Get hWndUserControl() As LongPtrWindow handle of the UserControl.
ToolTipText
Property Get ToolTipText() As String
Property Let ToolTipText(ByVal Value As String)Tooltip text.
Name
Property Get Name() As StringControl name. Read-only.
Tag
Property Get Tag() As String
Property Let Tag(ByVal Value As String)Custom data.
Parent
Property Get Parent() As ObjectParent object. Read-only.
Container
Property Get Container() As Object
Property Set Container(ByVal Value As Object)Container object.
Left
Property Get Left() As Single
Property Let Left(ByVal Value As Single)Left position.
Top
Property Get Top() As Single
Property Let Top(ByVal Value As Single)Top position.
Width
Property Get Width() As Single
Property Let Width(ByVal Value As Single)Width.
Height
Property Get Height() As Single
Property Let Height(ByVal Value As Single)Height.
Visible
Property Get Visible() As Boolean
Property Let Visible(ByVal Value As Boolean)Whether the control is visible.
HelpContextID
Property Get HelpContextID() As Long
Property Let HelpContextID(ByVal Value As Long)Help context ID.
WhatsThisHelpID
Property Get WhatsThisHelpID() As Long
Property Let WhatsThisHelpID(ByVal Value As Long)"What's This" help ID.
DragIcon
Property Get DragIcon() As IPictureDisp
Property Let DragIcon(ByVal Value As IPictureDisp)
Property Set DragIcon(ByVal Value As IPictureDisp)Drag icon.
DragMode
Property Get DragMode() As Integer
Property Let DragMode(ByVal Value As Integer)Drag mode.
Methods
AddItem
Public Sub AddItem(ByVal Item As String, Optional ByVal Index As Variant)Adds a list item.
RemoveItem
Public Sub RemoveItem(ByVal Index As Long)Removes the list item at the specified index.
Clear
Public Sub Clear()Clears all list items.
Refresh
Public Sub Refresh()Forces a redraw of the control.
SetSelRange
Public Sub SetSelRange(ByVal StartIndex As Long, ByVal EndIndex As Long)Sets the selection range (in multi-select mode).
SetColumnWidth
Public Sub SetColumnWidth(ByVal Value As Single)Sets the column width in multi-column mode.
SelectItem
Public Function SelectItem(ByVal Text As String, Optional ByVal Index As Long = -1) As LongSelects an item matching the text and returns the selected item index.
FindItem
Public Function FindItem(ByVal Text As String, Optional ByVal Index As Long = -1, Optional ByVal Partial As Boolean) As LongFinds an item matching the text and returns its index.
HitTest
Public Function HitTest(ByVal X As Single, ByVal Y As Single) As LongHit test, returns the item index at the specified coordinates.
HitTestInsertMark
Public Function HitTestInsertMark(ByVal X As Single, ByVal Y As Single, Optional ByRef After As Boolean) As LongInsertion mark hit test, returns the insertion position index.
ItemsPerColumn
Public Function ItemsPerColumn() As LongGets the number of items per column.
SelectedIndices
Public Function SelectedIndices() As CollectionReturns a collection of all selected item indices.
CheckedIndices
Public Function CheckedIndices() As CollectionReturns a collection of all checked item (checkbox/radio) indices.
GetIdealHorizontalExtent
Public Function GetIdealHorizontalExtent() As SingleGets the ideal horizontal scroll width.
OLEDrag
Public Sub OLEDrag()Initiates an OLE drag-drop operation.
Drag
Public Sub Drag(Optional ByRef Action As Variant)Starts, ends, or cancels a drag operation.
SetFocus
Public Sub SetFocus()Sets focus.
ZOrder
Public Sub ZOrder(Optional ByRef Position As Variant)Adjusts the Z-order.
Move
Public Sub Move(ByVal Left As Single, Optional ByVal Top As Variant, Optional ByVal Width As Variant, Optional ByVal Height As Variant)Moves and resizes the control.
Events
Click
Public Event Click()Click.
DblClick
Public Event DblClick()Double-click.
Scroll
Public Event Scroll()Fired when scrolling.
ItemCheck
Public Event ItemCheck(ByVal Item As Long)Fired when an item is checked or unchecked.
ItemBeforeCheck
Public Event ItemBeforeCheck(ByVal Item As Long, ByRef Cancel As Boolean)Fired before an item is checked or unchecked, can be canceled.
ItemMeasure
Public Event ItemMeasure(ByVal Item As Long, ByRef ItemHeight As Long)Fired when measuring item height in variable-height owner-draw mode.
ItemDraw
Public Event ItemDraw(ByVal Item As Long, ByVal ItemAction As Long, ByVal ItemState As Long, ByVal hDC As Long, ByVal Left As Long, ByVal Top As Long, ByVal Right As Long, ByVal Bottom As Long)Fired when drawing an item in owner-draw mode.
DropFiles
Public Event DropFiles(ByRef FileList As Variant, ByVal X As Single, ByVal Y As Single)Fired when files are dropped onto the control.
ContextMenu
Public Event ContextMenu(ByVal X As Single, ByVal Y As Single)Fired when a context menu is requested.
PreviewKeyDown
Public Event PreviewKeyDown(ByVal KeyCode As Integer, ByRef IsInputKey As Boolean)Preview key down event, fired before KeyDown.
PreviewKeyUp
Public Event PreviewKeyUp(ByVal KeyCode As Integer, ByRef IsInputKey As Boolean)Preview key up event, fired before KeyUp.
KeyDown
Public Event KeyDown(KeyCode As Integer, Shift As Integer)Key pressed.
KeyUp
Public Event KeyUp(KeyCode As Integer, Shift As Integer)Key released.
KeyPress
Public Event KeyPress(KeyChar As Integer)Key character.
MouseDown
Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)Mouse button pressed.
MouseMove
Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Mouse moved.
MouseUp
Public Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)Mouse button released.
MouseEnter
Public Event MouseEnter()Mouse entered the control.
MouseLeave
Public Event MouseLeave()Mouse left the control.
OLECompleteDrag
Public Event OLECompleteDrag(Effect As Long)OLE drag-drop completed.
OLEDragDrop
Public Event OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)OLE drag-drop dropped.
OLEDragOver
Public Event OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)OLE drag-drop hover.
OLEGiveFeedback
Public Event OLEGiveFeedback(Effect As Long, DefaultCursors As Boolean)OLE give feedback.
OLESetData
Public Event OLESetData(Data As DataObject, DataFormat As Integer)OLE set data.
OLEStartDrag
Public Event OLEStartDrag(Data As DataObject, AllowedEffects As Long)OLE drag started.
Code Examples
Basic Usage
' Add list items
ListBoxW1.AddItem "Item 1"
ListBoxW1.AddItem "Item 2", 0
' Set the currently selected item
ListBoxW1.ListIndex = 0
' Get selected item text
Dim s As String
s = ListBoxW1.TextCheckbox and Option Styles
' Checkbox style (set at design time)
' ListBoxW1.Style = LstStyleCheckbox
' Get checked items
Dim i As Long
For i = 0 To ListBoxW1.ListCount - 1
If ListBoxW1.ItemChecked(i) Then
Debug.Print ListBoxW1.List(i)
End If
Next iOwner-Draw
' Variable-height owner-draw (set DrawMode = LstDrawModeOwnerDrawVariable at design time)
Private Sub ListBoxW1_ItemMeasure(ByVal Item As Long, ByRef ItemHeight As Long)
ItemHeight = 30
End Sub
Private Sub ListBoxW1_ItemDraw(ByVal Item As Long, ByVal ItemAction As Long, _
ByVal ItemState As Long, ByVal hDC As Long, _
ByVal Left As Long, ByVal Top As Long, ByVal Right As Long, ByVal Bottom As Long)
' Custom drawing logic
End SubBatch Addition
' Disable redraw to speed up batch addition
ListBoxW1.Redraw = False
Dim i As Long
For i = 1 To 1000
ListBoxW1.AddItem "Item " & i
Next i
ListBoxW1.Redraw = True