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

Last reviewed: June 21, 1995
Article ID: Q80190
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

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  ==================
' Enter the following Declare as one, single line:
Declare Function SendMessage& Lib "user" (ByVal hWnd%, ByVal wMsg%,
   ByVal wParam%, ByVal lParam&)
Declare Function GetFocus Lib "User" () 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$ = "Derek is a great "

   ' You can scroll to see this portion.
   ListString2$ = "little boy "

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

   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


Additional reference words: 1.00 2.00 3.00 scrollbar
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.