|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Adding Ticks To A SliderThe Aesthetic Design Department is on the phone. The slider youve added to the program looks good, but whats 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 sliders possible values extend from 0 to 32767, and youve 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 its time to reset the TickFrequency property. To set the number of tick marks in a sliders scale, you actually set the distance between ticks with the TickFrequency property. For example, if your sliders scale goes from 0 to 100, a good value for the sliders TickFrequency might be 10 (although this depends on the sliders width or height, of coursea TickFrequency of 5 might be better for a long slider). You can set this property at design time or runtime. For example, heres 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.
Setting A Sliders Tick StyleThe Aesthetic Design Department is on the phone again. Your multimedia program is great, but wouldnt it be better if the sliders had tick marks on both sides? Well, you think, is that possible? It is. You can set a sliders 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:
For example, weve set TickStyle to sldBoth in the slider that appears in Figure 9.10.
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 Sliders Current ValueNow that youve added a new slider control to your program, how exactly can you determine that controls setting? As with scroll bars, you use the sliders Value property. The Value property is the sliders fundamental property. You can get or set the Value property at design time or runtime. For example, heres how we set a slider to a value of 125, halfway through its range of 0 to 250 (when you set a sliders 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 sliders knob, see the next two topics. Handling Slider EventsYouve 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 changessee the next topic in this chapter). You make use of the Change event to catch the users 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 sliders 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 sliders Value property:
Private Sub Slider1_Change()
Text1.Text = "Sliders position: " & Str(Slider1.Value)
End Sub
The result of this code appears in Figure 9.11. When the user moves the sliders knob, the sliders new setting appears in the text box. Now youre handling slider events.
Handling Continuous Slider EventsAlthough 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 sliders knob. In other words, the Change event lets you know what happened, whereas the Scroll event lets you know whats happening. Heres 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, well just display the sliders new setting in a text box, Text1:
Private Sub Slider1_Scroll()
Text1.Text = "Sliders position: " & Str(Slider1.Value)
End Sub
Note that unlike code using the Change event, this code updates the text box with the sliders new setting as the slider moves.
|
|
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. |