Numericupdown mousewheel事件会使十进制增加多于一个增量

Jon*_*onF 8 c# numericupdown mousewheel winforms

我试图覆盖鼠标滚轮控件,这样当鼠标滚轮向上或向下移动时,它只会将numericupdown字段中的值增加1.我相信它当前正在使用存储在控制面板中的内容并增加/减少每次值3.

我正在使用以下代码.即使numberOfTextLinesToMove只有1,我看到txtPrice.Value按预期填充,其他东西会覆盖它,因为我设置的值不是numericupdown框中显示的值

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);
            }

        }
Run Code Online (Sandbox Code Playgroud)

Eva*_*van 9

我最近遇到了这个问题,并通过改变增量来克服它.

numericUpDown1.Increment = 1m / SystemInformation.MouseWheelScrollLines;
Run Code Online (Sandbox Code Playgroud)

编辑:如果您只想使用鼠标滚轮更改值,这只是一个很好的解决方案.要解决所有情况,必须覆盖该类.这是一个简单的版本.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • +1为时尚的解决方案,但添加了边界检查。我使用了if(e.Delta&gt; 0 &amp;&amp;(this.Value + this.Increment)&lt;= this.Maximum)和else if(e.Delta &lt;0 &amp;&amp;(this.Value-this.Increment)&gt; = this最小) (2认同)

小智 7

用户3610013的答案效果很好。这是一个轻微的修改,可能对某些人来说很方便。

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));
}
Run Code Online (Sandbox Code Playgroud)


Pac*_*ccc 5

我接受了汉斯·帕桑(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));
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在查看了类似的问题后,我发现您也可以在不创建子类的情况下解决此问题 - 例如,此代码将为您提供一个 numericUpDown ,它只会滚动一(并且仅当控件具有焦点时)。

在容器的构造函数中设置委托:

numericUpDown1.MouseWheel += new MouseEventHandler(this.ScrollHandlerFunction);
Run Code Online (Sandbox Code Playgroud)

...然后在类的其他地方定义该函数:

private void ScrollHandlerFunction(object sender, MouseEventArgs e)
{
    HandledMouseEventArgs handledArgs = e as HandledMouseEventArgs;
    handledArgs.Handled = true;
    numericUpDown1.Value += (handledArgs.Delta > 0) ? 1 : -1;
}
Run Code Online (Sandbox Code Playgroud)

您还应该检查以确保您没有尝试滚动到控件的范围之外,因为这会崩溃。


Jay*_*ggs 0

这是此处报告的错误:NumericUpDown - 使用鼠标滚轮可能会导致不同的增量

Microsoft 在 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
} 
Run Code Online (Sandbox Code Playgroud)

  • 答案提到了 Visual Studio 2008,但请注意,这在 Visual Studio 2015 中并未修复。此外,错误报告链接已失效,我找不到它的任何档案。太糟糕了,我很想读它,我很好奇为什么鼠标滚轮事件不使用内置的“Increment”属性值。可能是 Raymond Chen 博客的好主题? (3认同)
  • 这是相当令人讨厌的代码,但我想它在大多数情况下都有效。不要将其放在可滚动面板上。最好的办法是从您可以从 Reflector 或参考源 UpDownBase.OnMouseWheel 方法获取的代码开始。只需将对 SystemInformation.MouseWheelScrollLines 的调用替换为 1 即可。 (2认同)
  • 我同意这很恶心,为什么不呢:`protected override void OnMouseWheel(MouseEventArgs e) {decimal newValue = Value; if (e.Delta &gt; 0) newValue += MouseWheelIncrement; 否则 newValue -= MouseWheelIncrement; if (newValue &gt; 最大值) newValue = 最大值; else if (newValue &lt; 最小值) newValue = 最小值; 值=新值;}` (2认同)