Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Jul 10, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 Then
Declare Function SendMessage Lib "user32" _
	Alias "SendMessageA" ( _
	ByVal hwnd As Long, ByVal wMsg As Long, _
	ByVal wParam As Long, lParam As Long) As Long
Declare 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 control

Public 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, _
	'Restore previous scale mode
	Frm.ScaleMode = nScaleMode
End Sub
Peter Gomis
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date