Vb.net – Vertical & Horizontal scrollbars in a panel

panelsscrollbarvb.net

Scenario:

  • Put a panel on a form.
  • Set the panel's BorderStyle to FixedSingle. (Just so you can see it when you run it.)
  • Set the panel's AutoScroll=True
  • Set the panel's Anchor to Top, Left, Bottom, Right
  • Inside the panel, place any sizable control (button, picturebox or whatever).
  • Adjust the control's bottom edge to be just a few pixels above the bottom of the panel.
  • Adjust the control's right edge should be a few pixels more narrow than the panel MINUS the width of a vertical scrollbar. (That is, it should be just narrow enough to leave room for a vertical scrollbar to may appear.)

Now run it, and vertically resize the form a little shorter so that you'd expect a VERTICAL scrollbar to appear.

Problem: BOTH scrollbars appear, because the very existence of the vertical scrollbar reduces the width of the client area, thus forcing a horizontal scrollbar to appear.

Apparently .NET evaluates whether a vertical scrollbar is necessary first, then evaluates whether the horizontal should appear, which is dependent upon whether the client size is reduced by the presence of a vertical scxrollbar. (i.e. the same experiment doesn't cause unnecessary VERTICAL scrollbars to appear… only horizontal ones.)

I'm using VB2008 Express but I'm guessing this carries over to later versions.

THE SOLUTION I NEED: I need either of: A) A "vertical autoscroll only" panel. B) I need a way to tell the panel to "rethink" whether the horizontal scrollbar is actually necessary. (Refreshes don't seem to do it.)

Best Answer

In order to use panel autoscroll property I do that:

  1. panel.AutoScroll = False (is inverse I know :D)
  2. panel.VerticalScroll.Visible = False or panel.HorizontalScroll.Visible = False

In order to know the dimensions of the scroolbars use

SystemInformation.HorizontalScrollBarHeight
SystemInformation.VerticalScrollBarWidth

So you can change the dimension of the panel when the scroolbar is shown.

Related Topic