How to disable scrolling on a NumericUpDown in a Windows Form
Asked Answered
T

2

6

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!

Turaco answered 22/1, 2020 at 15:1 Comment(1)
I'm not sure about the properties, but you could always create your own control that disables the mouse wheel, then replace all of the NumericUpDowns with your custom one. Not sure if that's the best option though.Hosanna
B
10

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.

Byebye answered 22/1, 2020 at 15:52 Comment(0)
F
3

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

Fording answered 22/1, 2020 at 15:37 Comment(1)
No this will also prevent change of the value when using arrowsDisagree

© 2022 - 2024 — McMap. All rights reserved.