Scroll and Change Events in the Colors Application

The Colors application demonstrates the difference between the two events. The two Picture Box controls, which display the color designed with the three scroll burs, react differently to the user’s actions. The first Pictureliox is updated from within the Scroll event, which occurs continuously as the user moves the indicator ofjl scroll bar. The second PictureBox is updated from within the Change event, which occurs after the indicator is moved to another position. As the user moves the indicator with the mouse, the first Picture Box displays the current.color and provides immediate feedback to the user. The other Picture- Box doesn’t follow the changes as they occur. When the mousebutton is released, a Change event occurs, and the second Picture Box is updated. At this point, both PictureBoxes display the color specified by the three scroll bars. If the user attempts.to change the Color value by clicking the two arrows of the scroll bars by clicking in the area to the left or to the right of the indicator, only the second PictureBox is updated. The user doesn’t slide the scroll bar, and therefore, no Scroll event is generated. Open the Colors project and experiment by changing the values of the ScrollBar controls with the-two techniques. The two, Picture Boxes can have different colors if you set a Color value by clicking the arrows of the scroll bars.The conclusion from this experiment is that you must program both the Scroll and the Change events to provide continuous feedback to the user. If this feedback requires too many calculations, which would slow down the reaction of the , Scroll event, program only the Change event. This event may not take place continuously, but it will take place after the ScrollBar’s value has changed, klike the Scroll event, which occurs only during the sliding of the control.

The Slider Control

The Slider control is similar to the ScrollBar control, but it lacks the granularity of the ScrollBar control. Suppose you want the user of an application to supply a value in a specific range, such as the speed of a moving object. Moreover, you don’t, . want to allow extreme precision; you need only a few settings, such as slow, fast, and very fast. A Siider control with three or four stops, such as the one shown earlier in Figure 5,14 will suffice. The user can set the control’s value by sliding the indicator or by clicking either side of the indicator.

The Slider control’s icon doesn’t appear on the Toolbox by default To use the control in a project you must right-click the Toolbox or select Components in the Project menu. On the Components dialog box, check Microsoft Window.s Common Controls 6.0. This will add a number of common controls to your Toolbox, including the Slider control. As with the ScrollBar control, Small Change and LargeChange properties are available. SmallChange is thesmallest increment by which the Slider value can change. The user can only change the slider by the SmallChange value by sliding the indicator (unlike the ScrollBar control, there are no arrows at the two ends of the Slider control). To change the Slider’s value by LargeChange, the user can dick either side of the indicator.
‘The Slider on the Form in Figure 5.14 (also see the Slider project on the CD) has its Min property set to 1, its Max property set to 5 (that is, five stops), and its TickStyle set-to sldNoTi cks (3). The code behind the control’s Change event, which sets the caption of the Label control at the bottom of the Form, is shown following.

The Slider Control’s Change Event

In the place of the tick marks under the Slider control are Label controls, which indicate the desired speed. In addition, each Label’s. Click event has also been programmed to change the position of the Slider control:

Private Sub Labe12_Click()
Slider1.Value = 2
End Sub

This simple subroutine enables you to make the Label a functional element of the program’s interface

VB6 at Work: The Inches Project

Figure 5.17 demonstrates another situation that calls for a Slider control. The Form in Figure 5.17 is an element of a program’s user interface that lets the user specify a distance between 0 and 10 inches and in increments of 0.1 inches. As the user slides the indieetor, the current value displays on a Label control, above the Slider. If you open the Inches application, you’ll notice that there are more stops than there are tick marks on the control. This is made possible with the TickFrequency property, which determines the frequency of the visible tick marks.

You may specify that the control has 50 stops (divisions) but that only 10 of them will be visible. The user can, however, position the ilidicator on any of the 40 invisible tick marks. You can think of the visible marks as the major tick marks ‘and the invisible ones as the minor tick marks. If the Tickfrequency propertyjs 5, only every fifth mark will be visible. The slider’s indicator, however, will slop at all tick marks.

You may specify that the control has 50 stops (divisions) but that only 10 of them will be visible. The user can, however, position the ilidicator on any of the 40 invisible tick marks. You can think of the visible marks as the major tick marks ‘and the invisible ones as the minor tick marks. If the Tickfrequency propertyjs 5, only every fifth mark will be visible. The slider’s indicator, however, will slop at all tick marks.

The slider needs to cover a range of 1O’inches in increments of 0.1 inches. To set the SmallChange property to 1, you have to set LargeChange to 10. Moreover, the TickFrequency is set to 5, so there will be a total of 10 divisions (corresponding to half and whole inches). The numbers below the tick marks were placed there withy.properly aligned Label controls. . The Label’s contents need to be updated as the Slider’s value changes. This is signaled with two events (which don’t occur simultaneously): the Change event, which occurs every time the value of the control changes, and the Scroll event, which occurs as the user slide? thecontrol’s indicator. While the Scroll event takes place, the Change event doesn’t, so you must program both events if you want to update the Label at all times. The code is the same and quite simple:

This single line of Visual Basic code must be inserted into the control’s Change event handler as well.

Scroll to Top