Search code examples
winformsmousewheelmdiparent

How to use MouseWheel to scroll MDI parent window in Winforms app


I have a VB.Net Winforms app which displays an MDI parent form and allows multiple child forms to be displayed on it. If the child forms extend beyond the screen height, a vertical scrollbar is automatically displayed on the right side of the MDI Parent & I can use this scrollbar to scroll the child forms into view.

But, the mousewheel has no effect on this scrollbar. How can I make the mousewheel scroll the child forms?

I can handle mousewheel events, but I am not sure what to do with them once I handle them to enable scrolling of the window.


Solution

  • Once you capture the mousewheel event, simply call SetDisplayRectLocation() of the control being scrolled. Something like

    myControl.SetDisplayRectLocation(
                myControl.DisplayRectangle.X, 
                myControl.DisplayRectangle.Y + MouseWheelDelta * ScrollAmount
              );
    

    (ScrollAmount is a constant you define -- start with 30 pixels).

    You also need to call AdjustFormScrollbars() on the main form as well to update the scroll bar location.

    (Sorry, that's C# -- I don't know VB syntax)