Microsoft KB Archive/80190

= How To Add a Horizontal Scroll Bar to Visual Basic List Box =

Article ID: 80190

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q80190



SUMMARY
The normal list box that comes with Visual Basic for Windows does not have a horizontal scroll bar. This can be a problem when the item in a list box extends past the boundaries of the list box. To add a horizontal scroll bar to the control, you can call the Windows API SendMessage function with the LB_SETHORIZONTALEXTENT (WM_USER + 21) constant.



MORE INFORMATION
To add a horizontal scroll bar to a list box, perform a SendMessage function call with the LB_SETHORIZONTALEXTENT constant.

This message sets the width in pixels by which a list box can scroll horizontally. If the size of the list box is smaller than this value, the horizontal scroll bar will horizontally scroll items in the list box. If the list box is large as or larger than this value, the horizontal scroll bar is disabled.

The parameters for the SendMessage function are as follows:

SendMessage(hWnd%, LB_SETHORIZONTALEXTENT, wParam%, lParam&)

  hWnd%   - Handle to the list box wParam% - Specifies the number of pixels by which the list box can be scrolled lParam% - Is not used

To make a program example that will only allow the user to scroll a specified distance, create a form with the following controls:

  Control             Name (CtlName in Visual Basic 1.0 for Windows) --  Command button      Command1 List box           List1

Add the following code in the described locations in your code:

'======== General Declarations for Form1 ================== ' For VB4 16-bit, add the keyword Private before each Declare, ' and enter the following Declare as one, single line: Declare Function SendMessage& Lib &quot;user&quot; (ByVal hWnd%, ByVal wMsg%,    ByVal wParam%, ByVal lParam&) Declare Function GetFocus Lib &quot;User&quot; as Integer

'======== Form1 ======================= 'NOTE: each command must appear on one, single line.

Sub Command1_Click Const LB_SETHORIZONTALEXTENT = &H400 + 21 Const NUL = 0& ' wParam is in PIXEL(3). ScaleMode = 3

' Get the handle. List1.SetFocus ListHwnd% = GetFocus

' This string will show up initially. ListString1$ = &quot;Derek is a great &quot;

' You can scroll to see this portion. ListString2$ = &quot;little boy &quot;

' You cannot scroll to see this string. ListString3$ = &quot;but can be a problem sometimes&quot;

ExtraPixels% = TextWidth(ListString2$) BoxWidth% = TextWidth(ListString1$)

' Resize the text box. List1.Move List1.Left, List1.Top, BoxWidth%

' Add the scroll bar. X& = SendMessage(ListHwnd%, LB_SETHORIZONTALEXTENT,        BoxWidth% + ExtraPixels%, NUL)

' Add the example string to the list box. List1.AddItem ListString1$ + ListString2$ + ListString3$ End Sub

