Option Explicit
Private Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const LB_SETHORIZONTALEXTENT = &H194
Public Sub AddHSBToListBox(sText As String, lst As ListBox)
Static x As Long
lst.AddItem sText
If x < TextWidth(sText & " ") Then
x = TextWidth(sText & " ")
End If
If ScaleMode = vbTwips Then
x = x / Screen.TwipsPerPixelX
SendMessageByNum lst.hwnd, LB_SETHORIZONTALEXTENT, x, 0
End If
End Sub
Contoh penggunaan menambah horizontal scrollbar pada listbox
Private Sub Command1_Click()
Dim sText As String
sText = ("This is a sample of long text, if the text longer than listbox, it will be create horizontal scrollbar automatically")
AddHSBToListBox sText, List1
End Sub