如何确保使用鼠标调整TrackBar控件将设置SmallChange的值的倍数,而不是两者之间的任何值? [英] How to ensure that adjusting a TrackBar control with a mouse will set values multiple of SmallChange and not any in between?
问题描述
我使用了.Net跟踪栏控件.我的轨迹栏点是0 ... 5 ... 10..15. 问题是,当用户滚动轨迹栏时,他们很容易在点之间放置滚动.但是我不希望该用户仅将滚动条放在我的显示点上.就像他们只设置了0,5,10等.不是他们设置了6,7,8,9 ...
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...
我将SmallChanges属性设置为5值.但是他们可以使用键盘更改,而不能使用鼠标滚动.
I set SmallChanges property with 5 value. But they working with keyboard changes not with mouse scrolling.
推荐答案
通过在ValueChanged事件的事件处理程序中覆盖Value属性,可以简单地将Value属性强制为SmallChange属性的倍数.像这样:
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.
这篇关于如何确保使用鼠标调整TrackBar控件将设置SmallChange的值的倍数,而不是两者之间的任何值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!