0

I have a FlowLayoutPanel that lays out the controls it contains top-down and then left-right such that normally it never has a vertical scrollbar, just a horizontal one. And so when you use the mouse wheel, it scrolls horizontally. Great.

Until the user grows a single control taller than the screen... then it has to have a vertical scrollbar. Fine. But as soon as the vertical scrollbar appears, the mouse wheel inputs start scrolling vertically. Bad.

So, is there any way to tell the FlowLayoutPanel or scrollbars that the mouse wheel should always mean horizontal? Or to tell it to go first to the horizontal bar and then to vertical only if no horizontal?

Brian Kennedy
  • 3,499
  • 3
  • 21
  • 27

2 Answers2

1

Subclass FlowLayoutPanel and override the OnMouseWheel method. Do not call the base method. Copy and paste the bottom half of the code from this method:

http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ScrollableControl.cs,c3f702621158871b

Loathing
  • 5,109
  • 3
  • 24
  • 35
  • Thanks for that suggestion. Looking at the code you pointed me to, I see the comment on the call to base.OnMouseWheel(e) that seems to imply that it is important that that is called last... so, not calling it sounds scary. Plus I hate copying base code into my derived class. But, based on this suggestion, I have crafted an alternative answer posted below. Thanks!!! – Brian Kennedy Nov 04 '15 at 16:16
0

Thanks to Loathing's excellent suggestion, which I have also marked as an answer, I chose to simply detect the case where both scroll bars are on and in that case temporarily turn off VScroll while I call the base class's method.

Why? Because I was worried about the consequences of NOT calling the base class's method given the comment on that code you pointed to... it is evidently important.

I don't see any flicker or other bad consequences from this solution, but please let me know if there are downsides that I should consider.

    protected override void OnMouseWheel(MouseEventArgs e)
    {
        if (this.PreferHorizontalScroll && this.VScroll && this.HScroll)
        {
            this.VScroll = false;
            base.OnMouseWheel(e);
            this.VScroll = true;
        }
        else
        {
            base.OnMouseWheel(e);
        }
     }
Brian Kennedy
  • 3,499
  • 3
  • 21
  • 27