Scrolling Controls with the ScrollBar Classes

Scrolling Controls with the ScrollBar Classes

It’s common practice for Windows applications to provide scroll bars as needed to display a control or form that’s larger than the area provided. This is commonly done for forms that contain documents, as seen in Microsoft Word or Microsoft Excel, or in controls such as list boxes and tree view controls.

Although the scroll bars that are embedded in controls such as the list box control don’t require programming support from you, you must manage any scroll bars that you add to forms or custom controls. Prior to the release of Visual C# .NET and the .NET Framework, using scroll bars in an application wasn’t a trivial task. However, with Visual C#, you can easily leverage the scroll bar classes included in the .NET Framework and easily add scrolling capabilities to your application.

The .NET Framework includes two scroll bar implementations. The HScrollBar class is used to create horizontal scroll bars, and the VScrollBar class is used to create vertical scroll bars. These classes are both derived from a common base class, ScrollBar.

Most of the time, you won’t need to create scroll bars explicitly. Many Windows Forms classes support automatic scrolling; these classes are derived from the ScrollableControl class. This class provides built-in support for scroll bars, allowing complete scroll bar functionality simply by setting a property. One of the classes derived indirectly from ScrollableControl is the Form class. Enabling basic automatic scrolling functionality for a form is as simple as setting the form’s AutoScroll property to true, as shown in the following code:

AutoScroll = true;

In addition to AutoScroll, two other properties affect scrolling behavior:

  • AutoScrollMargin  The minimum margin that’s required between child controls and the container’s edge. Scroll bars are created if needed to preserve this margin.

  • AutoScrollMinSize  The minimum size for the scrollable form or control. Scroll bars will be created automatically if the form or control is resized below this minimum size, even if no child controls have been obscured.

The AutoScrollMargin and AutoScrollMinSize properties have Size values. The Size structure is frequently used in Windows Forms classes to represent the dimensions of a rectangle, exposing two properties: Height and Width. A Size object is typically constructed as shown here:

Size minimumSize = new Size(150, 200);

The Size structure is discussed in more detail in Chapter 14. For now, it’s enough to understand that the Size structure is used with a form’s scrolling properties. By default, the AutoScrollMargin and AutoScrollMinSize properties have sizes with a height and width of 0. This triggers the creation of scroll bars only when a control is clipped by the edge of the form during a resizing operation.

If you prefer to establish some padding around the controls on your form, you can adjust the AutoScrollMargin property, as shown here:

AutoScrollMargin = new Size(50, 50);

Alternatively, you can use the following code to set the AutoScrollMinSize property to a minimum size for the scrolled region. If the dimensions of the form fall below this minimum size, scroll bars are displayed, even if no controls have been obscured.

AutoScrollMinSize = new Size(400, 400);

Part III: Programming Windows Forms