blindguy
blindguy

Reputation: 1009

How do I prevent mouse wheel from incrementing numericupdown without over loading?

How do I prevent mouse wheel from incrementing numericupdown without over loading?

I had previously inherited numericupdwon to overload the MouseWheel event with an empty event. This worked for a while, but something happened when I switched to x64 that made the whole inherited class periodically show not found. Not sure because even if I switched back to x86 it was still a problem.

Upvotes: 2

Views: 927

Answers (3)

Baddack
Baddack

Reputation: 2053

For C#: For some reason the MouseWheel doesn't show up in the list of events from the GUI. So I had to programmatically add the event in my form load. In the MouseWheel event I do something similar as the above selected answer (but a little different).

private void Form1_Load(object sender, EventArgs e)
{
    numericUpDownX.MouseWheel += new MouseEventHandler(handle_MouseWheel);
}

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

Upvotes: 0

Trevor_G
Trevor_G

Reputation: 1331

This worked for me..

Private Sub NumericUpDown1_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown1.MouseWheel

    Dim MW As HandledMouseEventArgs = CType(e, HandledMouseEventArgs)
    MW.Handled = True

End Sub

That HandledMouseEventArgs usage does look weird though.. but it works.

https://msdn.microsoft.com/en-us/library/system.windows.forms.handledmouseeventargs(v=vs.110).aspx

Upvotes: 3

blindguy
blindguy

Reputation: 1009

I came up with a different solution, using the following code to prevent the increment

Private Sub nup_cores_MouseWheel(sender As Object, e As MouseEventArgs) Handles nup_cores.MouseWheel
    nup_cores.Increment = 0
End Sub

And then change it back. In the specific case of a numericupdown, the cursor blinking seems to trigger gotfocus. The same principle could be applied with a short duration timer

Private Sub nup_cores_GotFocus(sender As Object, e As EventArgs) Handles nup_cores.GotFocus
    nup_cores.Increment = 1
End Sub

Upvotes: 0

Related Questions