How to ensure that adjusting a TrackBar control with a mouse will set values multiple of SmallChange and not any in between?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 03:53:46

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!