我正在尝试覆盖鼠标滚轮控件,这样当鼠标滚轮向上或向下移动时,它只会将numericupdown字段中的值增加1.我相信它当前正在使用存储在控制面板中的内容并增加/每次减少3个值。
我正在使用以下代码。即使numberOfTextLinesToMove只有1,我看到txtPrice.Value按预期填充,其他东西会覆盖它,因为我设置的值不是数字下拉框中显示的值
void txtPrice_MouseWheel(object sender, MouseEventArgs e)
{
int numberOfTextLinesToMove = e.Delta / 120;
if (numberOfTextLinesToMove > 0)
{
txtPrice.Value = txtPrice.Value + (txtPrice.Increment * numberOfTextLinesToMove);
}
else
{
txtPrice.Value = txtPrice.Value - (txtPrice.Increment * numberOfTextLinesToMove);
}
}
答案 0 :(得分:8)
我最近遇到了这个问题,并通过改变增量来解决这个问题。
numericUpDown1.Increment = 1m / SystemInformation.MouseWheelScrollLines;
编辑:如果您只想使用鼠标滚轮更改值,这只是一个很好的解决方案。要解决所有情况,必须覆盖该类。这是一个简单的版本。
public class NumericUpDownFix : System.Windows.Forms.NumericUpDown
{
protected override void OnMouseWheel(MouseEventArgs e)
{
HandledMouseEventArgs hme = e as HandledMouseEventArgs;
if (hme != null)
hme.Handled = true;
if (e.Delta > 0)
this.Value += this.Increment;
else if (e.Delta < 0)
this.Value -= this.Increment;
}
}
答案 1 :(得分:4)
我接受了Hans Passant的建议并将NumericUpDown
子类添加为添加此功能。它为OnMouseWheel方法使用Reflector代码的修改版本。以下是我对任何感兴趣的人的最终结果。
using System;
using System.ComponentModel;
using System.Windows.Forms;
/// <summary>
/// Extends the NumericUpDown control by providing a property to
/// set the number of steps that are added to or subtracted from
/// the value when the mouse wheel is scrolled.
/// </summary>
public class NumericUpDownExt : NumericUpDown
{
private int wheelDelta = 0;
private int mouseWheelScrollLines = 1;
/// <summary>
/// Gets or sets the number of step sizes to increment or
/// decrement from the value when the mouse wheel is scrolled.
/// Set this value to -1 to use the system default.
/// </summary>
[DefaultValue(1)]
[Description("Gets or sets the number of step sizes to increment or decrement from the value when the mouse wheel is scrolled. Set this value to -1 to use the system default.")]
public Int32 MouseWheelScrollLines {
get { return mouseWheelScrollLines; }
set {
if (value < -1)
throw new ArgumentOutOfRangeException("value must be greater than or equal to -1.");
if (value == -1)
mouseWheelScrollLines = SystemInformation.MouseWheelScrollLines;
else
mouseWheelScrollLines = value;
}
}
protected override void OnMouseWheel(MouseEventArgs e) {
HandledMouseEventArgs args = e as HandledMouseEventArgs;
if (args != null) {
if (args.Handled) {
base.OnMouseWheel(e);
return;
}
args.Handled = true;
}
base.OnMouseWheel(e);
if ((Control.ModifierKeys & (Keys.Alt | Keys.Shift)) == Keys.None && Control.MouseButtons == MouseButtons.None) {
if (mouseWheelScrollLines != 0) {
this.wheelDelta += e.Delta;
float num2 = (float)this.wheelDelta / 120f;
if (mouseWheelScrollLines == -1)
mouseWheelScrollLines = 1;
int num3 = (int)(mouseWheelScrollLines * num2);
if (num3 != 0) {
int num4;
if (num3 > 0) {
for (num4 = num3; num4 > 0; num4--)
this.UpButton();
this.wheelDelta -= (int)(num3 * (120f / (float)mouseWheelScrollLines));
} else {
for (num4 = -num3; num4 > 0; num4--)
this.DownButton();
this.wheelDelta -= (int)(num3 * (120f / (float)mouseWheelScrollLines));
}
}
}
}
}
}
答案 2 :(得分:4)
user3610013的答案非常有效。这是一个小小的修改,对某人来说可能很方便。
private void ScrollHandlerFunction(object sender, MouseEventArgs e)
{
NumericUpDown control = (NumericUpDown)sender;
((HandledMouseEventArgs)e).Handled = true;
decimal value = control.Value + ((e.Delta > 0) ? control.Increment : -control.Increment);
control.Value = Math.Max(control.Minimum, Math.Min(value, control.Maximum));
}
答案 3 :(得分:3)
在查看类似的问题后,我发现你也可以在不创建子类的情况下解决这个问题 - 例如,这段代码会给你一个只能滚动一个的numericUpDown(只有当控件有焦点时)。
在容器的构造函数中设置委托:
numericUpDown1.MouseWheel += new MouseEventHandler(this.ScrollHandlerFunction);
...然后在类的其他地方定义函数:
private void ScrollHandlerFunction(object sender, MouseEventArgs e)
{
HandledMouseEventArgs handledArgs = e as HandledMouseEventArgs;
handledArgs.Handled = true;
numericUpDown1.Value += (handledArgs.Delta > 0) ? 1 : -1;
}
你还应该检查以确保你没有试图滚出控件的范围,因为那会崩溃。
答案 4 :(得分:0)
根据Jay Riggs的回答,这是一个更清洁的版本(我认为):
namespace MyControls
{
public partial class NumericUpDown : System.Windows.Forms.NumericUpDown
{
public Decimal MouseWheelIncrement { get; set; }
public NumericUpDown()
{
MouseWheelIncrement = 1;
InitializeComponent();
}
protected override void OnMouseWheel(MouseEventArgs e)
{
decimal newValue = Value;
if (e.Delta > 0)
newValue += MouseWheelIncrement;
else
newValue -= MouseWheelIncrement;
if (newValue > Maximum)
newValue = Maximum;
else
if (newValue < Minimum)
newValue = Minimum;
Value = newValue;
}
}
}
答案 5 :(得分:0)
下面的代码将遍历您的整个表单,并修复每个多余的NumericUpDown控件。
将以下代码添加到您的类(或某些实用程序类)中:
static void FixNumericUpDownMouseWheel(Control c) {
foreach (var num in c.Controls.OfType<NumericUpDown>())
num.MouseWheel += FixNumericUpDownMouseWheelHandler;
foreach (var child in c.Controls.OfType<Control>())
FixNumericUpDownMouseWheel(child);
}
static private void FixNumericUpDownMouseWheelHandler(object sender, MouseEventArgs e) {
((HandledMouseEventArgs)e).Handled = true;
var self = ((NumericUpDown)sender);
self.Value = Math.Max(Math.Min(
self.Value + ((e.Delta > 0) ? self.Increment : -self.Increment), self.Maximum), self.Minimum);
}
然后在InitializeComponent()
后面的形式的构造函数中,插入行:
FixNumericUpDownMouseWheel(this);
答案 6 :(得分:-1)
这是此处报告的错误:NumericUpDown - use of mouse wheel may result in different increment
微软在2007年2月的回复表明他们无法解决这个Visual Studio 2008。
有两个发布的解决方法,两个都是子类NumericUpDown
。检查链接上的“变通方法”选项卡。
我试过的那个为我工作过(由'NanoWizard'发布):
using System;
using System.Windows.Forms;
internal class NumericUpDownControl : NumericUpDown
{
#region Constants
protected const String UpKey = "{UP}";
protected const String DownKey = "{DOWN}";
#endregion Constants
#region Base Class Overrides
protected override void OnMouseWheel(MouseEventArgs e_)
{
String key = GetKey(e_.Delta);
SendKeys.Send(key);
}
#endregion Base Class Overrides
#region Protected Methods
protected static String GetKey(int delta_)
{
String key = (delta_ < 0) ? DownKey : UpKey;
return key;
}
#endregion Protected Methods
}