Tip 138: Enabling and Disabling Scroll Bars in a List Box

July 1, 1995

Abstract

When you write a program in Visual Basic®, it may be necessary to temporarily enable or disable the scroll bars in a List Box control. This article explains how to use the Windows® application programming interface (API) GetSystemMetrics function with a Picture Box control to enable or disable the scroll bars in a List Box control.

Using the GetSystemMetrics Function

You can use the Windows® application programming interface (API) GetSystemMetrics function to retrieve the system metrics (for example, the width and height) of several elements of the Windows environment. To use this function in your Visual Basic® program, you must include the following Declare statement in the General Declarations section of your form (note that this Declare statement must be typed as a single line of code):

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long)
   As Long

The GetSystemMetrics function requires only one argument—a long value specifying the type of information you want to retrieve. The following table shows the constant values you must use to obtain the system metrics of a specific element in the Windows environment.

Const SM_CXSCREEN = 0 Width of screen.
Const SM_CYSCREEN = 1 Height of screen.
Const SM_CXFULLSCREEN = 16 Width of window client area.
Const SM_CYFULLSCREEN = 17 Height of window client area.
Const SM_CYMENU = 15 Height of menu.
Const SM_CYCAPTION = 4 Height of caption or title.
Const SM_CXFRAME = 32 Width of window frame.
Const SM_CYFRAME = 33 Height of window frame.
Const SM_CXHSCROLL = 21 Width of arrow bitmap on horizontal scroll bar.
Const SM_CYHSCROLL = 3 Height of arrow bitmap on horizontal scroll bar.
Const SM_CXVSCROLL = 2 Width of arrow bitmap on vertical scroll bar.
Const SM_CYVSCROLL = 20 Height of arrow bitmap on vertical scroll bar.
Const SM_CXSIZE = 30 Width of bitmaps in title bar.
Const SM_CYSIZE = 31 Height of bitmaps in title bar.
Const SM_CXCURSOR = 13 Width of cursor.
Const SM_CYCURSOR = 14 Height of cursor.
Const SM_CXBORDER = 5 Width of window frame that cannot be sized.
Const SM_CYBORDER = 6 Height of window frame that cannot be sized.

Const SM_CXDOUBLECLICK = 36 Width of rectangle around the location of the first click. The second click must occur in the same rectangular location.
Const SM_CYDOUBLECLICK = 37 Height of rectangle around the location of the first click. The second click must occur in the same rectangular location.
Const SM_CXDLGFRAME = 7 Width of dialog frame window.
Const SM_CYDLGFRAME = 8 Height of dialog frame window.
Const SM_CXICON = 11 Width of icon.
Const SM_CYICON = 12 Height of icon.
Const SM_CXICONSPACING = 38 Width of rectangles the system uses to position tiled icons.
Const SM_CYICONSPACING = 39 Height of rectangles the system uses to position tiled icons.
Const SM_CXMIN = 28 Minimum width of window.
Const SM_CYMIN = 29 Minimum height of window.
Const SM_CXMINTRACK = 34 Minimum tracking width of window.
Const SM_CYMINTRACK = 35 Minimum tracking height of window.
Const SM_CXHTHUMB = 10 Width of scroll box (thumb) on horizontal scroll bar.
Const SM_CYVTHUMB = 9 Width of scroll box (thumb) on vertical scroll bar.
Const SM_DBCSENABLED = 42 Returns a nonzero if the current Windows version uses double-byte characters, otherwise returns zero.
Const SM_DEBUG = 22 Returns nonzero if the Windows version is a debugging version.
Const SM_MENUDROPALIGNMENT = 40 Alignment of pop-up menus. If zero, left side is aligned with corresponding left side of menu bar item. If nonzero, left side is aligned with right side of corresponding menu bar item.
Const SM_MOUSEPRESENT = 19 Nonzero if mouse hardware is installed.
Const SM_PENWINDOWS = 41 Handle of Pen Windows dynamic-link library (DLL) if Pen Windows is installed.
Const SM_SWAPBUTTON = 23 Nonzero if the left and right mouse buttons are swapped.

In a Visual Basic application, you can selectively enable or disable the scroll bars in a List Box by first retrieving the width of the scroll bar's arrow bitmap. You do this by calling the GetSystemMetrics function with the value SM_CXVSCROLL for the Index argument. The width of the arrow bitmap is returned as a long value by the GetSystemMetrics function.

Next, a Picture Box control must be positioned directly over the scroll bars on the window or form in your project. The Width property of the Picture Box is set to the width of the arrow bitmap. By setting the Visible property of the Picture Box control to True, the user can employ the scroll bars in the usual manner. However, if you set the Visible property of the Picture Box control to False, the user cannot see the scroll bars. Therefore, the scroll bars become temporarily disabled.

Example Program

This program shows how to hide the scroll bars of a List Box control.

  1. Create a new project in Visual Basic. Form1 is created by default.

  2. Add the following Constant and Declare statements to the General Declarations section of Form1 (note that the Declare statement must be typed as a single line of code):
    Const SM_CXVSCROLL% = 2
    Private Declare Function GetSystemMetrics Lib "user32" 
       (ByVal nIndex As Long) As Long
    
  3. Add the following code to the Form_Load event for Form1:
    Private Sub Form_Load()
        Dim WD As Long
        List1.Visible = False
        WD = GetSystemMetrics(SM_CXVSCROLL%)
        
        Picture1.Width = WD * Screen.TwipsPerPixelX
        Picture1.Left = List1.Left + List1.Width - Picture1.Width
        
        Picture1.Top = List1.Top
        Picture1.Height = List1.Height
        Picture1.Line (0, 0)-Step(0, Picture1.Height)
        List1.Visible = True
        
        List1.AddItem "Vancouver"
        List1.AddItem "Seattle"
        List1.AddItem "London"
        List1.AddItem "Paris"
    End Sub
    
  4. Create a new procedure called ShowBar. Add the following code to this procedure:
    Sub ShowBar()
        Picture1.Visible = False
    End Sub
    
  5. Create a new procedure called HideBar. Add the following code to this procedure:
    Sub HideBar()
        Picture1.Visible = True
        Picture1.Line (0, 0)-Step(0, Picture1.Height)
    End Sub
    
  6. Add a List Box control to Form1. List1 is created by default. Set the following properties for List1:

    Height = 450
    Left = 2760
    Top = 1800
    Width = 1215

  7. Add a Picture Box control to Form1. Picture1 is created by default. Set the following properties for Picture1:

    AutoRedraw = True
    BorderStyle = 0-None
    Height = 450
    Left = 2760
    Top = 1800
    Visible = False
    Width = 1215

  8. Add a Command Button control to Form1. Command1 is created by default. Set its Caption property to "Show".

  9. Add the following code to the Click event for Command1:
    Private Sub Command1_Click()
        ShowBar
    End Sub
    
  10. Add a second Command Button control to Form1. Command2 is created by default. Set its Caption property to "Hide".

  11. Add the following code to the Click event for Command2:
    Private Sub Command2_Click()
        HideBar
    End Sub
    

Run the example program by pressing F5. Click the Hide command button. The scroll bars on the List Box control are no longer visible, but you can still scroll through the contents of the List Box with the arrow keys. Click the Show command button to show the scroll bars on the List Box control.

Additional Reference

"GetSystemMetrics." (MSDN Library Archive, Product Documentation, SDKs, Windows 3.1 SDK, Programmer's Reference Volume 2: Functions)