Display Horizontal Scrollbar

Display Horizontal Scrollbar

Unlike the Windows 95 common controls, the standard list box doesn’t have a horizontal scrollbar when list items are too wide to fit within the list box. Fortunately, it’s not hard to direct a list-box control to display a horizontal scrollbar.
Add this code to a form’s Load event. It fills a list box with 100 long strings and calls SetHScroll to show a horizontal scrollbar in the list box:

 Private Sub Form_Load()	Dim i As Integer	For i = 1 To 100		List1.AddItem CStr(i) & _			" bottle(s) of beer on the wall."	Next i	SetHScroll Me, List1, List1.List(0)End Sub

Add this code, which includes the required API declarations and the SetHScroll routine, to a BAS module. The SetHScroll routine uses the SendMessage API function to send the LB_SETHORIZONTALEXTENT message to a list box. The last argument is an item from the list, preferably one of the longest items. SetHScroll determines the string’s width in pixels and passes this value to the list box along with the LB_SETHORIZONTALEXTENT message. The list box sets its horizontal extent to this value, and if it is wider than the list-box control, the list box displays a horizontal scrollbar:

 #If Win32 ThenDeclare Function SendMessage Lib "user32" _	Alias "SendMessageA" ( _	ByVal hwnd As Long, ByVal wMsg As Long, _	ByVal wParam As Long, lParam As Long) As Long#ElseDeclare Function SendMessage Lib "user32" _	Alias "SendMessageA" ( _	ByVal hwnd As Integer, ByVal wMsg As Integer, _	ByVal wParam As Integer, lParam As Long) As Long#End If'Define constant for message to list-box controlConst LB_SETHORIZONTALEXTENT = &H194Public Sub SetHScroll(Frm As Form, Ctrl As _	Control, strText As String)	Dim nScaleMode As Integer	Dim nTextWidth As Integer	'Scale in pixels for window message	nScaleMode = Frm.ScaleMode	Frm.ScaleMode = 3	'Get the width, in pixels, of the text string	nTextWidth = Frm.TextWidth(strText)	'Send a message to the list box	SendMessage Ctrl.hwnd, _		LB_SETHORIZONTALEXTENT, nTextWidth, 0	'Restore previous scale mode	Frm.ScaleMode = nScaleModeEnd Sub
See also  Is Machine Learning Automating Creativity in Graphic Design?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist