1

I used .Net trackbar control. My trackbar point is 0...5...10..15. Problem is, when user scrolled trackbar they easily drop scroll in between points. But i wan't to that user only drop the scroll in my display point only. like they set only 0,5,10 etc. Not they set 6,7,8,9...

I set SmallChanges property with 5 value. But they working with keyboard changes not with mouse scrolling.

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
Jeet Bhatt
  • 744
  • 1
  • 7
  • 22

1 Answers1

2

You can simply force the Value property to be a multiple of the SmallChange property by overriding its value in an event handler for the ValueChanged event. Like this:

    private void trackBar1_ValueChanged(object sender, EventArgs e) {
        var bar = (TrackBar)sender;
        if (bar.Value % bar.SmallChange != 0) {
            bar.Value = bar.SmallChange * ((bar.Value + bar.SmallChange / 2) / bar.SmallChange);
        }
    }

Note that this even works while the user is dragging the thumb with the mouse, like it behaves when he uses the keyboard. I assumed that's what you wanted.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Interesting solution. There are some corner cases (like LargeChange not being a multiple of SmallChange will cause rounding problems). The simpler solution in http://stackoverflow.com/questions/23202679/force-trackbar-value-to-be-a-ten-multiple?lq=1 is also interesting and causes less user-code to be called. – Stéphane Gourichon Jan 29 '15 at 18:17