How to Set Automatic Scrolling On In a List Box In VB 3.0
ID: Q113329
|
The information in this article applies to:
-
Microsoft Visual Basic programming system for Windows, versions 1.0, 2.0, 3.0
SUMMARY
This article demonstrates two techniques for turning on automatic scrolling
inside a Visual Basic list box by using the SendMessage Windows API
function. This could be useful with database applications or any type of
application in which the user needs to be able to scroll a list box without
the use of the arrow keys.
MORE INFORMATION
Two different techniques are shown below.
Technique One
Behavior:
Once automatic scrolling is turned on, any vertical movement of the mouse
within the entire horizontal region of the desktop containing the list box
will cause the visual selection of an item in the list box. This includes
the capability of driving the scroll bar thumb to its upper and lower
limits.
Drawbacks:
With technique one, as long as the automatic scrolling is on, all mouse
movement on the desktop is mapped to the list box. Task switching, by using
the ALT-ENTER key combination, is disabled. Windows is basically waiting
for the completion of a click sequence. Any click anywhere on the desktop
will be directed to the Click event of the list box. The user will need to
double-click any other control to get the expected response from the
control. Double-clicking any other control also turns off the automatic
scrolling.
NOTE: For those programmers looking for a way to defeat the ability of the
user to task switch away from their application, this technique offers a
method.
Technique two
Behavior:
Once automatic scrolling is turned on, only movement of the mouse within
the list box will cause the visual selection of an item. The scroll bar
thumb will be driven downward once, but will not be driven back up. Mouse
clicks elsewhere on the desktop are processed normally and the automatic
scrolling remains on until the mouse is clicked in the list box.
Drawbacks:
The scroll bar of the list box is not driven in both directions based on
mouse movement because mouse movements outside the list box are not
processed by the list box.
Example of Setting Automatic scrolling for List box
- Start a new project in Visual Basic. Form1 is created by default.
- Add two Labels (Label1 and Label2), a Command button (Command1), and a
List box (List1) to Form1.
- Using the following table as a guide, set the properties of the controls
you added in step 2:
Control Name Property New Value
----------------------------------------------------------
Label1 Caption Turn off Auto-Scrolling by
clicking in the list box
Label1 Visible False
Label2 Visible False
Command1 Caption Turn on Auto-Scroll
- Place the following code in the (general) (declarations) section of
Form1:
' Enter the following Declare statement on one, single line:
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer,
ByVal wMsg As Integer, ByVal wparam As Integer,
ByVal lparam As Any) As Long
Dim tmp$
Dim autoselect_on As Integer
- Place the following code in the Form Load event procedure of Form1:
Sub Form_Load ()
list1.AddItem "test 1"
list1.AddItem "test 2"
list1.AddItem "test 3"
list1.AddItem "test 4"
list1.AddItem "test 5"
list1.AddItem "test 6"
list1.AddItem "test 7"
list1.AddItem "test 8"
list1.AddItem "test 9"
End Sub
Additional Steps for Technique One
- Place the following code in the List1 Click event procedure of Form1:
Sub List1_Click ()
tmp$ = list1.List(list1.ListIndex)
label2.Caption = "You selected: " & list1.List(list1.ListIndex)
label2.Visible = True
End Sub
- Place the following code in the Command1 Click event procedure of Form1:
Sub Command1_Click ()
Const WM_LBUTTONDOWN = &H201
' Turn on auto-scrolling:
r% = SendMessage(list1.hWnd, WM_LBUTTONDOWN, 0&, 0&)
label1.Visible = True
End Sub
- From the Run menu, choose Start (ALT, R, S), or press the F5 key
to run the program. Click the Command1 button. Move the mouse up
and down inside or outside the List1 box control. Double-click a
selection from the List1 box control to stop the automatic scrolling.
Additional Steps for Technique Two
- Place the following code in the List1 Click event procedure of Form1,
replacing the code referred to in step 6 above. The code in the
command button is no longer needed.
Sub List1_Click ()
Const WM_LBUTTONDOWN = &H201
tmp$ = list1.List(list1.ListIndex)
label2.Caption = "You selected: " & list1.List(list1.ListIndex)
label2.Visible = True
If Not autoselect_on Then
autoselect_on = True
r% = SendMessage(list1.hWnd, WM_LBUTTONDOWN, 0&, 0&)
Else
autoselect_on = False
End If
End Sub
- From the Run menu, choose Start (ALT, R, S), or press the F5 key to run
the program. Click the List1 box. Move the mouse up and down inside the
List1 box. Click again on a selection from the List1 box to stop the
automatic scrolling.
Additional query words:
2.00 3.00
Keywords :
Version :
Platform :
Issue type :
|