3

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();
}
Sam
  • 7,252
  • 16
  • 46
  • 65
Droes
  • 65
  • 1
  • 8
  • Every time the value of the track bar value is changed, get the value and see if it is equal to one of the increments you want, if not change the value to the closest increment? Ex. If you are incrementing by 10's and the value is changed to 12, set the value to 10. – AnotherUser Jun 18 '14 at 15:23
  • This is what I ended up doing, yes. Not really the way it should work I guess, but this does the job. – Droes Jun 20 '14 at 08:52

1 Answers1

4

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.

spyder1329
  • 656
  • 5
  • 15