Visual Basic Concepts
Using the Horizontal and Vertical Scroll Bar Controls
Scroll bars provide easy navigation through a long list of items or a large amount of information by scrolling either horizontally or vertically within an application or control. Scroll bars are a common element of the Windows and Windows NT interface.
Figure 7.24 The horizontal and vertical scroll bar controls
The horizontal and vertical scroll bar controls are not the same as the built-in scroll bars found in Windows or those that are attached to text boxes, list boxes, combo boxes, or MDI forms within Visual Basic. Those scroll bars appear automatically whenever the given application or control contains more information than can be displayed in the current window size (or, in the case of text boxes and MDI forms, when the ScrollBars property is also set to True).
In previous versions of Visual Basic, scroll bars were most often used as input devices. Windows interface guidelines now suggest, however, that slider controls be used as input devices instead of scroll bar controls. A slider control (of the type found in 32-bit Windows operating systems) is included in the Professional and Enterprise versions of Visual Basic.
Scroll bar controls are still of value in Visual Basic because they provide scrolling to applications or controls that do not provide them automatically. See "Scroll Bar Controls Scenario: Creating a Scrollable Graphics Viewport" for information on using scroll bars in this manner.
How the Scroll Bar Controls Work
The scroll bar controls use the Scroll and Change events to monitor the movement of the scroll box (sometimes referred to as the thumb) along the scroll bar.
Event | Description |
Change | Occurs after the scroll box is moved. |
Scroll | Occurs as the scroll box is moved. Does not occur if the scroll arrows or scroll bar is clicked. |
Using the Scroll event provides access to the scroll bar value as it is being dragged. The Change event occurs after the scroll box is released or when the scroll bar or scroll arrows are clicked.
The Value Property
The Value property (which, by default, is 0) is an integer value corresponding to the position of the scroll box in the scroll bar. When the scroll box position is at the minimum value, it moves to the leftmost position (for horizontal scroll bars) or the top position (for vertical scroll bars). When the scroll box is at the maximum value, the scroll box moves to the rightmost or bottom position. Similarly, a value halfway between the bottom and top of the range places the scroll box in the middle of the scroll bar.
In addition to using mouse clicks to change the scroll bar value, a user can also drag the scroll box to any point along the bar. The resulting value depends on the position of the scroll box, but it is always within the range of the Min to Max properties set by the user.
Note Min can be larger than Max if you want your scroll bar to display information changing from a larger to a smaller value.
The LargeChange and SmallChange Properties
To specify the amount of change to report in a scroll bar, use the LargeChange property for clicking in the scroll bar, and the SmallChange property for clicking the arrows at the ends of the scroll bar. The scroll bar's Value property increases or decreases by the values set for the LargeChange and SmallChange properties. You can position the scroll box at run time by setting Value between 0 and 32,767, inclusive.