Visual Basic Concepts

Using the Slider Control

See Also

A Slider control consists of a scale, defined by the Min and Max properties, and a "thumb," which the end user can manipulate using the mouse or arrow keys. At run time, the Min and Max properties can be dynamically reset to reflect a new range of values. The Value property returns the current position of the thumb. Using events such as the MouseDown and MouseUp events, the Slider control can be used to graphically select a range of values.

Possible Uses

TickStyle and TickFrequency Properties

The Slider control consists of two parts: the thumb and the ticks, as shown below:

The appearance of the control depends on the TickStyle property. In brief, the ticks can appear along the bottom of the control, as shown above (the default style), along the top, along both top and bottom, or not at all.

In addition to the placement of the ticks, you can also program how many ticks appear on the control by setting the TickFrequency property. This property, in combination with the Min and Max properties, determines how many ticks will appear on the control. For example, if the Min property is set to 0, the Max to 100, and the TickFrequency to 5, there will be one tick for every five increments, for a total of 21. If you reset the Min and Max properties at run time, the number of ticks can be determined by using the GetNumTicks method, which returns the number of ticks on the control.

Set the Min, Max Properties at Design Time or Run Time

The Min and Max properties determine the upper and lower limits of a Slider control, and you can set these properties at either design time or run time. At design time, right-click on the control and click Properties to display the Property Pages dialog box, as shown below:

At run time, you can reset the Min and Max settings to accommodate different ranges. For example, if you are using the Slider to change values in a database, you can use the same control and bind it to different fields depending on what table the user is editing.

SmallChange and LargeChange Properties

The SmallChange and LargeChange properties determine how the Slider control will increment or decrement when the user clicks it. The SmallChange property specifies how many ticks the thumb will move when the user presses the left or right arrow keys. The LargeChange property specifies how many ticks the thumb will move when the user clicks the control or when the user presses the PAGEUP or PAGEDOWN keys.

Selecting Ranges

If the SelectRange property is set to True, the Slider control changes its appearance, as shown below:

To select a range of values, you must use the SelStart and SelLength properties. For a detailed example of this, see "Slider Scenario 2: Select a Range of Values with the Slider" in this chapter.