Visual Basic > Forms
Setting the tab positions in a listbox
Setting the tab positions in a listbox To set the tab positions of a listbox use the following code: Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 'Purpose : Sets the tab positions in a listbox 'Inputs : lbListbox The listbox to set the tab positions ' alColWidths A zero based one dimension array of longs containing ' the new tab positions. Note, element zero must be 0 and ' the tab positions are cummulative. ' [lNumCols] The number of columns to alter the tab positions of. ' If this is less than the number of columns visible, the ' additional columns may be painted over. 'Outputs : Returns True on success, else returns False. 'Notes : When adding data to the listbox you must delimit your columns with ' the vbTab character eg. Me.List1.AddItem "Column1" & vbTab & "Column2" ' ' Example: For a listbox with three columns use equally spaced at 30 chars ' Dim lThisCol As Long, lSumColWidths As Long ' Dim alColWidths(0 To 2) As Long ' Const clMyColTabWidth as Long = 30 ' For lThisCol = 1 To UBound(alColWidths) ' alColWidths(lThisCol) = lSumColWidths + clMyColTabWidth 'Tab stops are cummulative ' lSumColWidths = lSumColWidths + alColWidths(lThisCol) ' Next ' ListboxSetTabStops Me.List1, alColWidths Public Function ListboxSetTabStops(lbListbox As ListBox, alColWidths() As Long, Optional lNumCols As Long = -1) As Boolean Const LB_SETTABSTOPS = &H192, WM_PAINT = &HF On Error GoTo ErrFailed If lNumCols = 0 Then 'Calculate the number of columns lNumCols = UBound(alColWidths) - LBound(alColWidths) + 1 End If 'Clear any existing tabs Call SendMessage(lbListbox.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&) 'Set new tabs Call SendMessage(lbListbox.hwnd, LB_SETTABSTOPS, lNumCols, alColWidths(0)) 'Refresh Listbox lbListbox.Refresh 'Return success code ListboxSetTabStops = True Exit Function ErrFailed: Debug.Print "Error in ListboxSetTabStops: " & Err.Description ListboxSetTabStops = False End Function
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows