Visual Basic Concepts
Another feature of the Slider control is the ability to select a range of values. In this implementation, when the user presses the SHIFT key while clicking on the Slider control, the MouseDown event occurs. Code in that event sets the SelectRange and SelStart properties. When the user releases the mouse button, the MouseUp event occurs, and in that code the SelLength property is set, from which a range of values can be extracted.
The code below uses the following objects:
To select a range of values with the Slider control
To enable the selection of a range of values, the SelectRange property must be set to True. One place to do this is the Form object's Load event, as shown below:
Private Sub Form_Load()
sldSelect.SelectRange = True
End Sub
Alternatively, you can set the property to True at design time by right-clicking on the control, and clicking on Properties to display the Property Pages dialog box.
In order to select a range, the user must hold down the SHIFT key while moving the slider's thumb. The MouseDown event has a shift argument, which allows you to determine if the SHIFT key is being held down. The If statement can be used to test for this possibility, as shown below:
Private Sub sldSelect_MouseDown _
(Button As Integer, Shift As Integer, _
x As Single, y As Single)
If Shift = 1 Then
' If the user has the Shift key down,
' handle it here.
End If
End Sub
If the SHIFT key is being held down by the user, the code then sets the SelStart and SelLength properties to appropriate values. The SelStart property specifies where a selection of values will begin. In the present context, the SelStart property would be set to where the thumb is placed — the Slider control's Value property.
The SelLength property specifies a range of values to select; this property begins at the SelStart value. In the MouseDown event, a new range is being selected, so any previous range must be deselected by setting the SelLength property to 0. This is shown in the code below:
sldSelect.SelStart = SldResize.Value
' Set previous SelLength (if any) to 0.
sldSelect.SelLength = 0
To select a range, the user must hold the SHIFT key down while dragging the mouse. The code to set the new range is therefore found in the MouseUp event, which occurs when the end user releases the slider thumb. The code below sets the SelLength property with a simple formula, the value of the thumb minus the SelStart property:
sldSelect.Value - sldSelect.SelStart
However, it is possible for the user to release the SHIFT key while selecting a range. In that case no selection should occur. Therefore the above code will only execute if the SHIFT key is still down. As with the MouseDown event, an If statement can test for this possibility:
Private Sub sldSelect_MouseUp _
(Button As Integer, Shift As Integer, _
x As Single, y As Single)
If Shift = 1 Then
' If user selects backwards from a point,
' an error will occur.
On Error Resume Next
' Else set SelLength using SelStart and
' current value.
sldSelect.SelLength = _
sldSelect.Value - sldSelect.SelStart
Else
'If user lifts SHIFT key, set SelLength
' to 0 (to deselect the SelRange) and exit.
sldSelect.SelLength = 0
End If
End Sub
The complete code is shown below:
Private Sub Form_Load()
sldSelect.SelectRange = True
End Sub
Private Sub sldSelect_MouseDown _
(Button As Integer, Shift As Integer, _
x As Single, y As Single)
If Shift = 1 Then
sldSelect.SelStart = sldSelect.Value
' Set previous SelLength (if any) to 0.
sldSelect.SelLength = 0
End If
End Sub
Private Sub sldSelect_MouseUp _
(Button As Integer, Shift As Integer, _
x As Single, y As Single)
If Shift = 1 Then
' If user selects backwards from a point,
' an error will occur.
On Error Resume Next
' Else set SelLength using SelStart and
' current value.
sldSelect.SelLength = _
sldSelect.Value - sldSelect.SelStart
Else
'If user lifts SHIFT key, set SelLength
' to 0 (to deselect the SelRange) and exit.
sldSelect.SelLength = 0
End If
End Sub