How To Add a Horizontal Scroll Bar to Visual Basic List Box (80190)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0
  • Microsoft Visual Basic Standard Edition for Windows 1.0

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 "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
				

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

192184 : How To Add a Horizontal Scrollbar to Visual Basic ListBox


Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbhowto KB80190