Creating a scrollable container is straightforward, since the control class
inherits from System.Windows.Forms.ScrollableControl
. This class
exposes several useful properties
AutoScroll
This determines if scroll bars should be displayed
if there is a control in the container that is outside the bounds of the
client rectangle.
AutoScrollMinSize
This determines the minimum size of the client
rectangle so that scroll bars do not appear. If the client rectangle is smaller
than this, then there will be scroll bars.
AutoScrollMargin
This sets the minimum space between any child
control and a scroll bar. It ensures that there will always be space between
child controls and the scroll bar.
DisplayRectangle
This is a read-only property that returns the
smallest rectangle that will contain all of the controls in the container.
This is calculated by the size and location of the controls in the container.
If the size or location are changed, then the DisplayRectangle
will be recalculated. If there are no controls in the container, then the
DisplayRectangle
will equal the client rectangle.
It is possible to make the main form a scrollable container, but there is a problem with adding scroll bars and a status bar: the scroll bars will be added below the status bar. This means that the status bar could be scrolled out of view. This is not the normal behaviour for a status bar.
The next attempt is to add the status bar, then add a scrollable panel whose docking is set to full.
Drawing to this panel has problems because the DisplayRectangle
will always be the same as the ClientRectangle
, so the scroll bars
will never appear, unless additional controlls are added to the panel.
If this panel is undocked, then the scroll bars will hide the status bar.
The final attempt is to add a new panel to the scrolling panel. Set the size
of this panel to the size of the desired drawing area. The
DisplayRectangle
will be set to the size of this panel. Scroll
bars will appear whenever the client rectangle is smaller than the panel.
Be sure to send the status bar and toolbar to the back, so that they are
behind the scrolling panel.
The size of the line scroll is 5 pixels.
The size of the page scroll is equal to the size of the client rectangle.