2

Let's have a large Control (whose Size is given externally and "cannot" be altered). This Control is contained in Panel. There is very useful feature Panel.Autoscroll = true, which enables comfortable scrolling of the large Control with zero work.

There is one problem: The Control needs to handle the MouseWheel (and it gets the events as expected - it is focused on mouse enter). Ufortunately the Panel also captures the same mouse wheel events and uses them for scrolling. This is not desirable for the cases the Control "consumes" the events.

I cannot find a standard way to handle this. Even the Panel.Scroll event is called only on mouse click scrolls, not mouse wheel scrolls.

Is there a natural WinForms friendly way how to temporarily instruct the Panel with AutoScroll set to true not to process the mouse wheel events for scrolling?

sharpener
  • 1,383
  • 11
  • 22
  • The problem is that the control does *not* consume the event. It uses the event but then lets it bubble to its parent. Backgrounder [is here](http://stackoverflow.com/a/6987339/17034). – Hans Passant Oct 14 '14 at 13:15
  • Yes, I knew it doesn't, but as there is no flag in the MouseEventArgs to signal it, it raised my question. How (the hell ;) you do know it actually IS HandledMouseEventArgs (and thus it contains the consumed flag)? It's just enough to cast it (safely) and set the Handled for the case of consumption. Thanks! – sharpener Oct 14 '14 at 14:28
  • 1
    http://referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/winforms/Managed/System/WinForms/Control.cs – Hans Passant Oct 14 '14 at 14:48
  • Wasn't aware of this. Even better than ILSpy. – sharpener Oct 14 '14 at 14:55

1 Answers1

2

Replace your panel with the AutoScroll = True with this version, which comments out the base call to the OnMouseWheel method:

public class PanelEx: Panel {
  protected override void OnMouseWheel(MouseEventArgs e) {
    //base.OnMouseWheel(e);
  }
}

or as Hans points out from his linked answer, set the Handled property to true in your control:

protected override void OnMouseWheel(MouseEventArgs e) {
  base.OnMouseWheel(e);
  ((HandledMouseEventArgs)e).Handled = true;
}
Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 2
    Doesn't stop the bubbling, now the form scrolls :) Proper fix is ((HandledMouseEventArgs)e).Handled = true; – Hans Passant Oct 14 '14 at 13:16
  • No need to derive new Panel. It's enough to cast it in the default implementation inside the mouse wheel event handler. – sharpener Oct 14 '14 at 14:29