Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Scrollbars

Scrollbars let users control value changes. Rather than type specific values, the user can move the scrollbars with the mouse to specify relative positions within a range of values. The toolbox includes both a Horizontal Scrollbar and a Vertical Scrollbar control.

Table 11.2 contains a list of important scrollbar properties that determine the behavior of the scrollbar.

Table 11.2. Fundamental scrollbar properties.

Property Description
LargeChange Specifies the amount that the scrollbar changes when the user clicks within the scrollbar's shaft area.
Max Indicates the maximum number of units that the scrollbar value represents at its highest setting. The range is from 1 to 32,767 (the default Max value).
Min Indicates the minimum number of units the scrollbar value represents at its lowest setting. The range is from 1 (the default Min value) to 32,767.
SmallChange Specifies the amount that the scrollbar changes when the user clicks an arrow at either end of the scrollbar.
Value Contains the unit of measurement currently represented by the position of the scrollbar.

When you place a scrollbar on a form, you must decide at that time what range of values the scrollbar is to represent. The scrollbar's full range can extend from 1 to 32,767. Set the Min property to the lowest value you want represented by the scrollbar. Set the Max property to the highest value you want represented by the scrollbar.

When the user eventually uses the scrollbar, the scrollbar arrows control small movements in the scrollbar's value, determined by the SmallChange property. Clicking the empty part of the shaft on either side of the scrollbox produces a positive or negative change in the value represented by the LargeChange property. The user can drag the scrollbox itself to any position within the scrollbar shaft to jump to a specific location instead of changing the value gradually.

Suppose, for example, that a horizontal scrollbar represented a range of whole dollar amounts from $5 to $100. When the user clicks the scroll arrows, the scrollbar's value changes by $1. When the user clicks the empty shaft on either side of the scrollbox, the scrollbar's value changes by $5. Here are the property values that you would set that determine how VB interprets each click of the scrollbar: Min: 5, Max: 100, SmallChange: 1, and LargeChange: 5.

The physical size of the scrollbar has no bearing on the scrollbar's returned values when the user selects from the scrollbar. Adjust the scrollbars on your form so that the scrollbars are wide enough or tall enough to be appropriately sized for the items that they represent.

The thumb is the scrollbar's moving scrollbox (the elevator-like box).

Figure 11.7 shows an application that uses a vertical scrollbar to change the size of a label's font size. As the user clicks the top scrollbar arrow, the font size shrinks by the SmallChange value. As the user clicks the bottom scrollbar arrow, the font size increases by the SmallChange value. (The application's SmallChange property value is 1.) If the user clicks in the scrollbar's shaft on either side of the scrollbar's thumb, the LargeChange property value of 5 is either added to or subtracted from the font size.

11fig07.gif

Figure 11.7 The vertical scrollbar determines the label's font size.

Listing 11.2 shows the code behind Figure 11.7. The code isn't lengthy because the scrollbar's Click() event procedure must change only the label's text font size and the label that displays the current font size. Any time the user changes the scrollbar, the scrollbar's Change() event procedure executes.

Example 11.2. The code behind the scrollbar application.

1: Private Sub vsbHeight_Change()
2: lblScroll.FontSize = vsbHeight.Value
3: lblFontHeight.Caption = vsbHeight.Value
4: End Sub

Share ThisShare This

Informit Network