Sandeep CompGeek Sandeep CompGeek - 2 months ago 21
Vb.net Question

How to write mouse wheel scroll event when wheel (middle) button held down?

Can someone guide me to achieve this requirement in

vb.net
??
Algorithm:

If middle click pressed AND mouse wheel scroll up
number ++;
else
number--;


I tried this but it doesn't work:

If e.Button = MouseButtons.Middle and e.Delta > 0


I wanted something like a combination of Mouse Down and Mouse Wheel events which detects both middle click and mouse wheel scrolling.

Answer

You need to handle more than one event. You need to capture the MouseWheel event and the MouseDown/MouseUP events.

Private mouseDown As Boolean = False
Private Sub Form1_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
    If e.Delta > 0 And mouseDown Then
        Console.WriteLine("Scrolling with button down")
    Else
        Console.WriteLine("Button Not down whilst scrolling")
    End If
End Sub

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
    If e.Button = MouseButtons.Middle Then
        mouseDown = True
    End If
End Sub

Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
    If e.Button = MouseButtons.Middle Then mouseDown = False
End Sub

Write to a private variable when the middle mouse is down, then run your condition statement within the wheel handler.