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
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a