Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSelect a range with the Slider control
Keywordsslider, range
CategoriesControls
 
Set the Slider's SelectRange property to True. In the Slider's MouseDown event handler, save the Slider's initial value. Call the SelectRange subroutine to select a zero-length range. Also set an m_Selecting flag indicating that a selection is in progress.

In the Slider's MouseMove event handler, see if the Shift key is pressed. If it is, call SelectRange to select the range between the Slider's initial and current values. If Shift is not pressed, use SelectRange to select a zero-length range.

In the Slider's MouseUp event handler, set m_Selecting to False to indicate the selection is over.

The SelectRange subroutine selects a range between two numbers where either may be larger than the other. The Slider control crashes if its SelLength property is less than zero so the routine may need to switch the numbers.

 
Option Explicit

Private m_SelStart As Integer
Private m_Selecting As Boolean

Private Sub Form_Load()
    Slider1.SelectRange = True
End Sub

' Start selecting a range.
Private Sub Slider1_MouseDown(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    m_Selecting = True

    ' Save the initial value for ranges.
    m_SelStart = Slider1.Value

    ' Initially the range has zero length whether Shift is
    ' pressed or not.
    SelectRange m_SelStart, m_SelStart
End Sub

' Continue selecting a range.
Private Sub Slider1_MouseMove(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    If Not m_Selecting Then Exit Sub

    ' See if Shift is pressed.
    If Shift And vbShiftMask Then
        ' Shift is pressed. Select a range.
        SelectRange m_SelStart, Slider1.Value
    Else
        ' Shift is not pressed. Select no range.
        SelectRange Slider1.Value, Slider1.Value
    End If
End Sub

' Finish selectign a range.
Private Sub Slider1_MouseUp(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    m_Selecting = False
End Sub

' Select a slider range where v1 may be smaller than v2.
Private Sub SelectRange(ByVal v1 As Integer, ByVal v2 As _
    Integer)
    If v1 <= v2 Then
        If Slider1.SelStart <> v1 _
            Then Slider1.SelStart = v1
        If Slider1.SelLength <> v2 - v1 _
            Then Slider1.SelLength = v2 - v1
    Else
        If Slider1.SelStart <> v2 _
            Then Slider1.SelStart = v2
        If Slider1.SelLength <> v1 - v2 _
            Then Slider1.SelLength = v1 - v2
    End If
End Sub
 
My book Custom Controls Library explains how to build your own ActiveX controls using Visual Basic 5 and 6. This is more work but would allow you to wrap all this code inside the control and would give you the ability to modify the control in other ways.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated