Slider Scenario 2: Select a Range of Values with the Slider

See Also

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

  1. Set the Slider control's SelectRange property to True.

  2. In the MouseDown event, test to see if the shift key is down.

  3. In the MouseUp event, set the SelLength property to Value - SelStart.

Set the Slider Control's SelectRange Property to True

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.

MouseDown Event: Test to See if the Shift Key is Down

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

MouseDown Event: Set the SelStart and SelLength Properties

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 

MouseUp Event: Set the SelLength to the Value - SelStart Property

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

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