Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Handling Slider Selections

Using the Shift key, you can select a range of values in a slider. From the users’ point of view, the process goes like this: they move the slider’s knob to the beginning of the selection they want to make in a slider and press the Shift key. Then they move the knob to the end of the range they want to select and release the Shift key. When the Shift key is released, the selection appears in the slider as a blue band.

This capability of sliders is useful when you want to specify a range—for example, you might want to set the tolerable level of music volume to a certain range. To let a slider select a range, you must first set the SelectRange property to True (when it’s False, the slider will not support range selection). Here are the two properties you use when selecting ranges in sliders:

  SelLength returns or sets the length of a selected range in a slider control.
  SelStart returns or sets the start of a selected range in a slider control.

However, setting the range when the user uses the Shift key is up to you. Let’s see how that can work in a simple example. We’ll need some way of determining if the Shift key is up or down in this example, so we set up a form-wide Boolean variable, blnShiftUp, in the (General) declarations area of the form:

Dim blnShiftUp As Boolean

And we set that variable to True when the form loads:

Private Sub Form_Load()
    Slider1.Max = 250
    Slider1.Min = 0
    Slider1.LargeChange = 5
    Slider1.TickFrequency = 25
    blnShiftUp = True
End Sub

When users move the knob to the beginning of the range they want to select and press the Shift key, we can catch that in the KeyDown event handler for the slider; here, we check if the Shift argument is 1, which means the Shift key is down:

Private Sub Slider1_KeyDown(KeyCode As Integer, Shift As Integer)

    If Shift = 1 And blnShiftUp Then
...
    End If

End Sub

(The Shift argument in KeyUp and KeyDown event handlers is a bit field, with the least-significant bits corresponding to the Shift key [bit 0], the Ctrl key [bit 1], and the Alt key [bit 2]. These bits correspond to the values 1, 2, and 4, respectively.)

If the Shift key is down, we set the flag blnShiftUp to False; we set the start of the selection, SelStart, to the current slider position; and we set the length of the selection, SelLength, to 0. (Note that it’s necessary to set the length of the selection to 0 in case the user starts further selections after finishing with the current one):

Private Sub Slider1_KeyDown(KeyCode As Integer, Shift As Integer)

    If Shift = 1 And blnShiftUp Then
        blnShiftUp = False
        Slider1.SelStart = Slider1.Value
        Slider1.SelLength = 0
    End If

End Sub

Now when a key goes up, we check to make sure the Shift key is up in the KeyUp event handler:

Private Sub Slider1_KeyUp(KeyCode As Integer, Shift As Integer)

    If Shift = 0 Then
...
    End If

End Sub

If the Shift key is indeed up, we set the Boolean flag blnShiftUp to True, place the selection length in SelLength (note that we use the Visual Basic absolute value, Abs(), function here to find the selection length, because the user may have moved the slider’s knob to a lower, not higher, setting), and set the SelStart property to the current value of the slider if that value is less than the current SelStart:

Private Sub Slider1_KeyUp(KeyCode As Integer, Shift As Integer)

    If Shift = 0 Then
        blnShiftUp = True
        Slider1.SelLength = Abs(Slider1.Value - Slider1.SelStart)
        If Slider1.Value < Slider1.SelStart Then
            Slider1.SelStart = Slider1.Value
        End If
...

Finally, we can display the length of the new selection in a text box this way:

Private Sub Slider1_KeyUp(KeyCode As Integer, Shift As Integer)

    If Shift = 0 Then

        blnShiftUp = True
        Slider1.SelLength = Abs(Slider1.Value - Slider1.SelStart)
        If Slider1.Value < Slider1.SelStart Then
            Slider1.SelStart = Slider1.Value
        End If

        Text1.Text = "Selection length: " & Str(Slider1.SelLength)
    End If

End Sub

And that’s it. When you run this program and make a selection with the slider, the length of that selection appears in the text box, as in Figure 9.12.


Figure 9.12  Selecting a range in a slider.

Clearing A Selection In A Slider

Besides setting selections in sliders, you can also clear them with the ClearSel method. For example, here’s how we might set up a selection in a slider when the form holding that slider loads:

Private Sub Form_Load()
    Slider1.Max = 250
    Slider1.Min = 0
    Slider1.LargeChange = 5
    Slider1.TickFrequency = 25
    Slider1.SelStart = 30
    Slider1.SelLength = 10
End Sub

And here’s how we can clear that selection when the user clicks a command button:

Private Sub Command1_Click()
    Slider1.ClearSel
End Sub

That’s all there is to it.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.