The ScroUBar and Slider controls let the user specify a magnitude by scrolling a selector between its minimum and maximum values. In some situations, the user doesn’t know in advance the exact value-of the quantity to specify (in which case, a textbox would suffice), so your application must provide a more flexible mechanism for specifying a value, along with some type o~visual feedback. The vertical scroll bar that lets a user move up and down a long document is a typical example of the use-of a Scrollbar control. In the past, users had to supply’ line numbers to locate the section of the document they wanted to view. With a highly visual operating system, however, this is no k lng~r even an option. The scroll bar and visual feedback are the prime mechanisms for repositioning the view. in a long document or in a large picture tha t won’t fit entirely in its window. When scrolling through a document or an image to locate the area of interest,’ the user doesn’t know or care about line numbers or pixel coordinates. Rather, the user uses the scroll bar to navigate through the document and the visible part of the document provides the required feedback. The example in Figure 5.13 was created with the ScrolPic application.
The Slider control is similar to the ScrollBar control, but it doesn’t cover a continuous range of values: The Slider control has a fixed number.of tick marks, which the developer can label (e.g., Off, Slow, Speedy,). The user can place the slider’s indicator to the desired value. While the ScrollBar control relies on some visual feedback outside the control to help the user position the indicator to the desired value, the Slider control forces the user to select from; a range of valid values.
In short, the ScrollBar control should be used when the exact value isn’t as important as the value’s effect on another object or data element. The Slider control should be used when the user can type a numeric value and the value your application expects is a number in a specific range; for example, integers between a and 100, or a value between a and 5 inches in steps of 0.1 inches (0.0, 0.1, 0.2 inches, and so on, up to 5 inches). The Slider control is preferred to the TextBoxcontrol in similar situations because there’s no need for data validation on your part. The user can only specify valid numeric values with the mouse.
The ScroliBar Control
The ScrollBar control is a long stripe with an indicator that lets the user select a value between the two ends of the control. ‘The ScrollBar control comes in two versions: horizontal and vertical. Other than their orientation, there are no differences . between the two versions. The left (or bottom) end of the control corresponds to its minimunrvalue. The other end is the control’s maximum value. The current value of the control is determined by the position of the indicator, which can be scrolled between the minimum and maximum values. The basic properties of the ScrollBar control, therefore, are properly named Min, Max, and Value.
• Min The control’s minimum value
• Max The control’s maximum value
• Value The control’s current value, specified by the indicator’s position
The Min and Max properties are positive Integer values, which means the valid range of values for a ScrollBar control is 0 to 2655. To cover a range of negative numbers or nonintegers, you must supply the code to map the actual values to Integer values. For example, to cover a range from 2.5 to 8.5, set the Min property to 25, set the Max property to 85, and divide the control’s value by 10. If the range you need is from -2.5 to 8.5; set the Min property to 0 and the Max property to 110 (25 + 85). This time you’d not only divide the result by 10, you’d also have to subtract 2.5 from it to map the control’s zero value to -2.5.
VB6 at Work: The Colors Project
Another example that demonstrates how the ScrollBar control works. The Colors application lets the user specify a color by manipulating the value of its basic colors (red, green, and blue) through scroll bars. Each basic color is controlled by a scroll bar and they all have minimum values of 0 and maximum values of 255.
As the scroll bar is moved, the corresponding color is displayed, and the can easily specify a color without knowing the exact values of its primary nents. All the user needs to know is whether the desired color containsto red or too little green. With the help of the scroll bars and. the immediate from the application, the user can easily pinpoint the exact value. Notice to 11exact value” is,of no practical interest; only the final color counts. Scroll bars and slider bars have minimum and maximum values that can with the Min and Max properties. The indicator’s position in the control cmines ifs value, which is set or read with the Value property. In the Color cation, the initial value of the control is set to 128 (the-middle of the range looking at the code for the Colors application, let’s examine the control’s.
The Scroll Bar Control’s Events
The user can change the ScroliBar control’s value in three ways:
• By clicking the two arrows at its ends. The value of the control changes amount specified with the.5″mallChange property.
• By clicking the area between the indicator and the arrows. The value of the, control changes by the amount specified with the LArgeChange property.
• By scrolling the indicator with the mouse.
You can monitor the changes on the ScrollBar’s value-from within your with two events: Change and Scroll.
• Change The Change eventoccurs every time the user changes the indicator’s position and releases the mouse button. While the indicator is being moved, the Change event isn’t triggered. For instance, if the user clicks on the indicator’s button and holds down the mouse button while moving the mouse back and forth, no Change event is triggered. The Change event occurs only when the mouse button is released. .
• Scroll The Scroll event occurs continuously while the indicator 1::> moving. This lets you update other controls on the Form from within your code as the user moves the indicator with the mouse. After the mouse button is released, the control stops triggering Scroll events and triggers a single Change event because the control’s value has changed. In most situations, you should program both events to react to user actions.