Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:


How to set the interval for the track-bar in C# winforms instead of continuous numbers?

The pointer should jump to 1,5,10,15 instead of 1,2,3,4,5...15.

I have tried to set the SmallChange=5 in the Properties of TrackBar but it doesn't change the interval while dragging the pointer.

Can anyone please help to fix this problem?
Posted
Updated 8-May-14 18:55pm
v2
Comments
Ram Kumar(Webunitech) 9-May-14 1:10am    
Just use the timers and make a small logic for getting the number difference.

The 'Value Property of the TrackBar is continuous from 'MinimumValue to 'MaximumValue.

To simulate detents (snap-to-values) you need to implement a TrackBar 'ValueChanged EventHandler like this:
C#
private int smallChangeValue = 5;
private int trackValue;
private bool blockRecursion = false;

private void trackBar1_ValueChanged(object sender, EventArgs e)
{
    if (blockRecursion) return;

    trackValue = trackBar1.Value;

    if (trackValue % smallChangeValue  != 0)
    {
        trackValue = (trackValue / smallChangeValue) * smallChangeValue;

        // see note #1 below
        blockRecursion = true;

            trackBar1.Value = trackValue;

        blockRecursion = false;
    }

    // for debugging only
    // textBox1.Text = trackValue.ToString();
}
If you examine what's happening here, you can see this is just doing a simple form of rounding-off (rounding-down actually) of the TrackBar value.

note #1: in this example the setting a flag to block recursion is not really necessary; the reason this is shown is in case you want to do a bit more sophisticated handling such as: examining the modulo remainder of value and small-change value, and, in case it's closer to the next value, rounding-up.
 
Share this answer
 
just change TickFrequency properties
 
Share this answer
 
Comments
Member 9794576 9-May-14 20:29pm    
TickFrequency is not the solution to my above stated problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900