Beneagle
Beneagle

Reputation: 103

How to disable scrolling on a NumericUpDown in a Windows Form

I have been trying to find a way to disable the scrolling of a NumericUpDown when I accidentally mouse-over a NumericUpDown. Is there a way to do this using the properties?

I found a way to disable it using code for a combobox, but not directly within the Properties panel in the Designer view.

This was the code I found:

    void comboBox1_MouseWheel(object sender, MouseEventArgs e) {
        ((HandledMouseEventArgs)e).Handled = true;
    }

I found the code from this link:

C# - how do I prevent mousewheel-scrolling in my combobox?

I have a lot of these NumericUpDowns in my Design, and would therefore like to have a way to just disable the mouse scrolling when I accidentily mouse-over the NumericUpDown. So if there is a way to do this directly in the Properties, it would help a lot. Thanks in advance!

Upvotes: 6

Views: 2710

Answers (2)

LocEngineer
LocEngineer

Reputation: 2917

You cannot set this in designer properties. But you can use a little cheat code to do this:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control ctl in Controls)
    {
        if (ctl.GetType() == typeof(NumericUpDown))
            ctl.MouseWheel += Ctl_MouseWheel;
    }
}

private void Ctl_MouseWheel(object sender, MouseEventArgs e)
{
    ((HandledMouseEventArgs) e).Handled = true;
}

I just tested this and it did the job nicely.

Upvotes: 10

FonnParr
FonnParr

Reputation: 335

If you do not need the Up and Down buttons to do anything, then you can set the Increment property to 0

Upvotes: 3

Related Questions