Turn off mousewheel event.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Qwert

    #1

    Turn off mousewheel event.

    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.


  • Larry Lard

    #2
    Re: Turn off mousewheel event.


    Qwert wrote:[color=blue]
    > 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?[/color]

    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(By Val 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 InitializeCompo nent) from

    Me.NumericUpDow n1 = New System.Windows. Forms.NumericUp Down
    to
    Me.NumericUpDow n1 = 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.

    --
    Larry Lard
    Replies to group please

    Comment

    Working...