Droes
Droes

Reputation: 75

Specific values/intervals on a Trackbar

What I'm trying to do here is put down a trackbar like the one on Windows XP to change resolution: (http://puu.sh/7Li5h.png)

I want to set specific intervals/increment values like in the picture above. Currently the lines underneath the actual bar are there, but I can still move the pointer everywhere I like. This is my current code:

trackBarIP.Minimum = 0;
trackBarIP.TickFrequency = 1000;
trackBarIP.SmallChange = 50;
trackBarIP.LargeChange = 100;
trackBarIP.Maximum = 6300;

I have this code to show the current value of the Trackbar in the textbox next to it:

(http://puu.sh/7Ligk.png)

private void trackBarIP_ValueChanged(object sender, EventArgs e)
{
    textBoxIP.Text = trackBarIP.Value.ToString();
}

Upvotes: 4

Views: 7269

Answers (1)

spyder1329
spyder1329

Reputation: 745

I know this is a really old post but below is my solution:

It uses a C# trackbar in Visual Studio 2013 and the "Scroll" event.

        zoomTrackBar.Minimum = 25;
        zoomTrackBar.Maximum = 400;
        zoomTrackBar.Value = 100;
        zoomTrackBar.TickFrequency = 25;
    }
    #endregion

    private void zoomTrackBar_Scroll(object sender, EventArgs e)
    {
        int value = (sender as TrackBar).Value;
        double indexDbl = (value * 1.0) / zoomTrackBar.TickFrequency;
        int index = Convert.ToInt32(Math.Round(indexDbl));

        zoomTrackBar.Value = zoomTrackBar.TickFrequency * index;

        label2.Text = zoomTrackBar.Value.ToString();
    }

All it does is take the current selected value and divides it by the frequency, the hash marks (in my case 25). I then round this number up and that is my "hash index." From here I can easily calculate the correct hash by multiplying this "index" by my frequency. The last step is to set the trackbar equal to the new value.

Upvotes: 6

Related Questions