Turn off mousewheel event.

Q

Qwert

Hello,

when a NumericUpDown control has the focus, the mousewheel
increases/decreases its value. How does one prevent this from happening? How
does one turn off its mousewheel event?

Thanks.
 
L

Larry Lard

Qwert said:
Hello,

when a NumericUpDown control has the focus, the mousewheel
increases/decreases its value. How does one prevent this from happening? How
does one turn off its mousewheel event?

Because a MouseEventArgs doesn't have a Cancel, we have to do a little
more work. This is one way, if there's a less fiddly way someone will
be along soon enough to tell you about it :)

What we do here is derive our own class from NumericUpDown, and
override the handling of the MouseWheel event. First define our class:

Friend Class MyNumericUpDown
Inherits NumericUpDown

Protected Overrides Sub OnMouseWheel(ByVal e As MouseEventArgs)
'do nothing
End Sub

End Class

Note that the 'normal' behaviour of a method in a derived class is to
first do its base class's behavior, then do its own. Here we explicitly
do nothing when a mouse wheel event occurs.

Now you just need to use this new control instead of the regular
NumericUpDown. A quick way of doing this is to just place a normal
NumericUpDown on your form, then in the auto-generated code section,
change two things:

Change the declaration of the control from

Friend WithEvents NumericUpDown1 As NumericUpDown
to
Friend WithEvents NumericUpDown1 As MyNumericUpDown

and change the creation of the control (in InitializeComponent) from

Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown
to
Me.NumericUpDown1 = New MyNumericUpDown

Because a MyNumericUpDown is still a NumericUpDown, you are still able
to use the visual designer as normal.

As I said, I'm not sure if this is all the best way to do this, but it
works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top