#675 – Handling the PreviewMouseWheel and MouseWheel Events

Every user interface element defines the PreviewMouseWheel/MouseWheel event pair, which fire whenever the user interacts with the mouse wheel.

The PreviewMouseWheel event is a tunneling event, firing on each element of the logical tree, from the top down.  The corresponding MouseWheel event is bubbling, firing up the logical tree from the control where the event originated.

The mouse wheel events will fire once for each click of the mouse wheel.  The MouseWheelEventArgs.Delta property is meant to indicate the amount that the mouse wheel changed, but will in practice take on one of two values–a positive value when the mouse wheel is rotated forward (away from the user) and a negative value otherwise.

        private static int eventCount = 0;

        private void win1_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Info = string.Format("Click #{0}, Delta = {1}", ++eventCount, e.Delta);
        }

Rotating the wheel forward, we get a Delta of 120.

Rotating backward, we get -120.

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #675 – Handling the PreviewMouseWheel and MouseWheel Events

  1. Pingback: Dew Drop – October 24, 2012 (#1,428) | Alvin Ashcraft's Morning Dew

Leave a comment