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


Adding Ticks To A Slider

The Aesthetic Design Department is on the phone. The slider you’ve added to the program looks good, but what’s that thick black bar underneath it? You explain that sliders use tick marks to make it easier to move the knob to the approximate position that the user wants. In this case, the slider’s possible values extend from 0 to 32767, and you’ve just added a tick mark for each unit on that scale. That would give you 32,767 tick marks, they say. Right, you say. Maybe it’s time to reset the TickFrequency property.

To set the number of tick marks in a slider’s scale, you actually set the distance between ticks with the TickFrequency property. For example, if your slider’s scale goes from 0 to 100, a good value for the slider’s TickFrequency might be 10 (although this depends on the slider’s width or height, of course—a TickFrequency of 5 might be better for a long slider).

You can set this property at design time or runtime. For example, here’s how we set the tick frequency in a slider to 10 units:

Private Sub Form_Load()
    Slider1.Max = 255
    Slider1.Min = 0
    Slider1.TickFrequency = 10
End Sub

The result of this code appears in Figure 9.9.


Figure 9.9  Setting tick frequency in a slider control.


TIP:  To make the tick marks come out evenly spaced, you should set the TickFrequency value so that the equation (Max - Min) / TickFrequency comes out to be a whole number with no remainder. To find out how many ticks there are in a slider, use its GetNumTicks() method.

Setting A Slider’s Tick Style

The Aesthetic Design Department is on the phone again. Your multimedia program is great, but wouldn’t it be better if the sliders had tick marks on both sides? Well, you think, is that possible?

It is. You can set a slider’s TickStyle property to sldBoth to place tick marks on both sides of a slider. In fact, you can place ticks on one side, both sides, or no sides of a slider. Here are the possible values of the TickStyle property:

  sldBottomRight—0; ticks on bottom or right only
  sldTopLeft—1; ticks on top or left only
  sldBoth—2; ticks on both sides
  sldNoTicks—3; no ticks

For example, we’ve set TickStyle to sldBoth in the slider that appears in Figure 9.10.


Figure 9.10  A slider with ticks on both sides.

You can also set the TickStyle property in code. Here, we set TickStyle to sldNoTicks when a slider loads:

Private Sub Form_Load()
    Slider1.Max = 100
    Slider1.Min = 0
    Slider1.LargeChange = 5
    Slider1.TickFrequency = 10
    Slider1.TickStyle = sldNoTicks
End Sub

Getting A Slider’s Current Value

Now that you’ve added a new slider control to your program, how exactly can you determine that control’s setting? As with scroll bars, you use the slider’s Value property.

The Value property is the slider’s fundamental property. You can get or set the Value property at design time or runtime. For example, here’s how we set a slider to a value of 125, halfway through its range of 0 to 250 (when you set a slider’s Value in code, the knob in the slider moves to match):

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

To work with the Value property when the user moves the slider’s knob, see the next two topics.

Handling Slider Events

You’ve added the new slider to your program, and it looks fine. But how do you connect it to your code? How can you make sure that the slider events are handled properly when the user uses it?

Like scroll bars, sliders have a Change event (and like scroll bars, they also have a Scroll event to handle continuous changes—see the next topic in this chapter). You make use of the Change event to catch the user’s slider actions.

An example will make this clearer; here, we set up a slider when the form loads, setting its Min, Max, and other properties:

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

When the user is done moving the slider’s knob, a Change event occurs, which you can catch in a Change event handler:

Private Sub Slider1_Change()

End Sub

For example, we can display the current setting of the slider in a text box this way, using the slider’s Value property:

Private Sub Slider1_Change()
    Text1.Text = "Slider’s position: " & Str(Slider1.Value)
End Sub

The result of this code appears in Figure 9.11. When the user moves the slider’s knob, the slider’s new setting appears in the text box. Now you’re handling slider events.


Figure 9.11  Handling slider events.

Handling Continuous Slider Events

Although sliders have a Change event, the Scroll event might be a better choice when working with a slider. The Change event only occurs when users complete their slider actions, but Scroll events occur as users move the slider’s knob. In other words, the Change event lets you know what happened, whereas the Scroll event lets you know what’s happening.

Here’s an example. We set up a slider, Slider1, when the form containing that slider loads, like this:

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

Then we can catch slider actions by setting up an event handler for the Scroll event:

Private Sub Slider1_Scroll()

End Sub

In this case, we’ll just display the slider’s new setting in a text box, Text1:

Private Sub Slider1_Scroll()
    Text1.Text = "Slider’s position: " & Str(Slider1.Value)
End Sub

Note that unlike code using the Change event, this code updates the text box with the slider’s new setting as the slider moves.


TIP:  Of course, the Scroll event is not appropriate for all cases. For example, if you have an action that needs a firm setting before getting started, it might be better to use the Change event. However, providing visual feedback to users as they move a slider using Scroll can prove very useful.


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.