12

マウスホイールを上下に動かすと、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);
            }

        }
4

7 に答える 7

13

最近この問題が発生し、インクリメントを変更することで回避しました。

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;
    }
}
于 2013-05-02T12:25:31.673 に答える
6

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));
}
于 2014-09-05T19:25:29.177 に答える
5

私はHansPassantのアドバイスを受け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));
                    }
                }
            }
        }
    }
}
于 2013-01-10T17:57:15.177 に答える
1

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;
        }
    }
}
于 2014-12-17T07:36:55.177 に答える
-1

これは、ここで報告されたバグです: NumericUpDown - マウス ホイールを使用すると、増分が異なる場合があります

2007 年 2 月の Microsoft の回答では、この Visual Studio 2008 に対処できないと述べています。

サブクラス化された 2 つの回避策が投稿されてい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
} 
于 2011-03-08T00:10:28.683 に答える