9

I need to know how to determine the scroll whether is forward or backward(not vertical or horizontal) on MouseWheel event.

panel1.MouseWheel += ZoomIn

public void ZoomIn(object sender, EventArgs e)
{
     // Need to know whether the wheel is scrolled forwards or backwards
}

2 Answers 2

15

Instead of EventArgs use MouseEventArgs which expose the Delta property. It is negative on down scrolling and positive on upscrolling.

panel1.MouseWheel += ZoomIn;

public void ZoomIn(object sender, MouseEventArgs e)
{
    if(e.Delta > 0)
    {
        // The user scrolled up.
    }
    else
    {
        // The user scrolled down.
    }
}
3
  • Worked! Thanks a lot Mar 28, 2018 at 8:09
  • 1
    Update for WheelEventArgs in .NET 6 it becomes DeltaY, if (e.DeltaY > 0) for zoom out / scroll down if (e.DeltaY < 0) for zoom in / scroll up
    – user160357
    Apr 4, 2022 at 1:15
  • e.deltaY works for me
    – Barrard
    Aug 28, 2022 at 10:57
4

Per MSDN:

When handling the MouseWheel event it is important to follow the user interface (UI) standards associated with the mouse wheel. The MouseEventArgs.Delta property value indicates the amount the mouse wheel has been moved. The UI should scroll when the accumulated delta is plus or minus 120. The UI should scroll the number of logical lines returned by the SystemInformation.MouseWheelScrollLines property for every delta value reached. You can also scroll more smoothly in smaller that 120 unit increments, however the ratio should remain constant, that is SystemInformation.MouseWheelScrollLines lines scrolled per 120 delta units of wheel movement.

So you want to change the type of e in your handler to be MouseEventArgs (or introduce a cast), and then use its Delta property

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.