Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (16 Votes)
Automatically add a horizontal scrollbar to a listbox, quickly and completely. I wasnt satisfied with the ones on planet so I created my own.
API DeclarationsPublic Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const LB_SETHORIZONTALEXTENT = &H194
Public Sub AddScroll(List As ListBox)
Dim i As Integer, intGreatestLen As Integer, lngGreatestWidth As Long
'Find Longest Text in Listbox
For i = 0 To List.ListCount - 1
If Len(List.List(i)) > Len(List.List(intGreatestLen)) Then
intGreatestLen = i
End If
Next i
'Get Twips
lngGreatestWidth = List.Parent.TextWidth(List.List(intGreatestLen) + Space(1))
'Space(1) is used to prevent the last Character from being cut off
'Convert to Pixels
lngGreatestWidth = lngGreatestWidth \ Screen.TwipsPerPixelX
'Use api to add scrollbar
SendMessage List.hwnd, LB_SETHORIZONTALEXTENT, lngGreatestWidth, 0
End Sub