|
|
 |
 |
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 9 Scroll Bars And Sliders
- If you need an immediate solution to:
- Adding Horizontal Or Vertical Scroll Bars To A Form
- Setting Scroll Bars Minimum And Maximum Values
- Setting Up Scroll Bar Clicks (Large Changes)
- Setting Up Scroll Bar Arrow Clicks (Small Changes)
- Getting A Scroll Bars Current Value
- Handling Scroll Bar Events
- Handling Continuous Scroll Bar Events
- Showing And Hiding Scroll Bars
- Coordinating Scroll Bar Pairs
- Adding Scroll Bars To Text Boxes
- Creating And Using Flat Scroll Bars
- Customizing Flat Scroll Bar Arrows
- Creating Slider Controls
- Setting A Sliders Orientation
- Setting A Sliders Range
- Setting Up Slider Groove Clicks
- Adding Ticks To A Slider
- Setting A Sliders Tick Style
- Getting A Sliders Current Value
- Handling Slider Events
- Handling Continuous Slider Events
- Handling Slider Selections
- Clearing A Selection In A Slider
- Creating An Updown Control
- Setting An Updown Controls Minimum And Maximum
- Handling Updown Events
In Depth
In this chapter, were going to take a look at those controls that scroll and slide in Visual Basic. The controls well cover here are scroll bars, sliders, flat scroll bars, and updown controls, shown in Figure 9.1. Every Windows user is familiar with scroll bars. If computers had wall-sized displays, we might not need scroll bars, but as it is, scroll bars help control what parts of your programs data are visible at any one time. For example, you can place a large document in a text box, only part of which is visible at once. Using scroll bars, you can manipulate the document, moving through it as you like. You manipulate that document by dragging the small box in the scroll bar, which is called the scroll bars thumb. A relatively new control is the flat scroll bar, which functions just like a normal scroll bar, except that it can appear flat, rather than three-dimensional.
Figure 9.1 Scroll bars, a flat scroll bar, a slider, and an updown control.
A new control for some Windows user is the slider control, which appears at the bottom of Figure 9.1. Using the mouse, you can drag the knob in a slider control much the way youd work the volume control on a stereo. You use slider controls to let the user make a selection from a range of values in a convenient way. For example, you may use a slider control to resize an image rather than asking the user to type in twip values.
The updown control is also new to many users. This control consists of two buttons, one pointing up and one pointing down, as you see at right in Figure 9.1. Updowns actually work much like the arrow buttons in scroll bars, because each time you click them, the setting of the control changes. You use updowns to let the user increment or decrement a setting.
Adding Scroll Bars And Sliders To A Program
Standard scroll bars are intrinsic controls in Visual Basic, which means that they appear in the toolbox as soon as you start Visual Basic. Youll find both the Vertical and the Horizontal Scroll Bar tools in the toolbox; to add those controls to a form, just paint them as you need them in that form.
You add the other controls in this chapter with the Project|Components menu item (click the Controls tab in the Components box that opens). To add flat scroll bars, you select the Microsoft Flat Scrollbar Control item; to add sliders, you select the Microsoft Windows Common Controls item; and to add the updown control, you click the Microsoft Windows Common Controls-2 item.
The toolbox tools for these controls appear in Figure 9.2. The Horizontal Scroll Bar tool is fourth down in the middle, the Vertical Scroll Bar tool is fourth down on the right. The Updown tool is eighth down in the middle, the Slider tool is eleventh down on the right, and the Flat Scroll Bar tool is twelfth down in the middle.
Figure 9.2 The Horizontal Scroll Bar, Vertical Scroll Bar, Updown, Slider, and Flat Scroll Bar tools.
In overview, these controls work in more or less the same way: you add them to a form, use Min and Max properties to set the possible range of values the user can set, then read the Value property to get the controls setting in a Change event handler to interpret actions undertaken by the user.
Change events occur after the user is finished changing the controls setting; you can also use the Scroll event to handle events as the user works with the control, as well see in this chapter. In fact, well see how all this works and more in the Immediate Solutions, and well turn to that now.
Immediate Solutions
Adding Horizontal Or Vertical Scroll Bars To A Form
Many programmers think that there is one Scroll Bar tool that you add to a form and then set its orientationvertical or horizontal. In fact, those are two different controls, as you see in the toolbox in Figure 9.2. To add a horizontal scroll bar to a form, you use the Horizontal Scroll Bar tool, and to add a vertical scroll bar, you use the Vertical Scroll Bar tool. A horizontal scroll bar, HScroll1, and a vertical scroll bar, VScroll1, appear in Figure 9.3.
Figure 9.3 A horizontal and a vertical scroll bar.
Setting Scroll Bars Minimum And Maximum Values
The Testing Department is calling again. The Field Testing Unit loves the new program youve written to help them record in-the-field performance of the companys products, but theres just one problem: performance is measured on a scale of 1 to 100, and the scroll bars in your program seem to go from 0 to 32767. Its been very hard for the users of your program to operate with only 1/32 of the whole scroll bar. Can you rescale it?
Yes, you can. After you place a scroll bar in a program, the first thing to do is to set its range of possible values, which by default is 0 to 32767. The minimum value a scroll bar can be set to is stored in its Min property, and the maximum value in the Max property. You can set the Min and Max properties for scroll bars at design time or at runtime; heres how we change those properties in a vertical scroll bar:
Private Sub Form_Load()
VScroll1.Min = 1
VScroll1.Max = 100
End Sub
|