Search code examples
vb.netwinformscontrolsnumericupdown

Using mouse to increment and decrement value of the NumericUpDown control in VB.net


I have a NumericUpDown control in my VB.Net 2.0 project. When the user clicks the mouse inside the control and drags left or right, I want the value of the control to increment or decrement accordingly.

The problem I am having is that I am not able to capture the mouse-move event once the mouse pointer is inside the boundaries of the control and the mouse button is pressed. I've tried overriding the onMouseMove() event, but this event only fires once when the mouse is moved over the text-box part of the NumericUpDown control. However, the event fires continuously when moved over the Up/Down buttons of the same control.

I still want to preserve the normal operation of the control: be able to type in a value, use the up and down button to change the value etc...

Anyone have any suggestions what to try? Thanks!


Solution

  • The mouse event goes to the TextBox that's inside the NUD. You can get a reference to it from Controls property, index 1 is the textbox:

    Public Sub New()
        InitializeComponent()
        AddHandler NumericUpDown1.Controls(1).MouseDown, AddressOf NudMouseDown
        AddHandler NumericUpDown1.Controls(1).MouseMove, AddressOf NudMouseMove
    End Sub
    

    This kinda breaks the rules but its okay, that control is never going to change anymore.