Classes: TScrollBar

When a control is configured to display a list of items and many lines of text, that control gets equipped with bars that allow to user to navigate either left and right or up and down as needed. These bars are called scroll bars. Delphi allows you to position your own scroll bars almost anywhere on a form.

A scroll bar is a control that allows the user to navigate in two directions along the control by clicking the mouse on the appropriate arrow. There are two forms of the scroll bars. The horizontal scroll bar allows the user to scroll left and right while the vertical scroll bar would scroll up and down.

Scroll Bars Properties

To add a scrollbar control on the form, click the ScrollBar button on the Standard tab of the Component Palette.

A ScrollBar control can assume one of two orientations: horizontal or vertical. The orientation property is set using the TSCrollBarKind enumerator. By default, a newly added ScrollBar is horizontal. To control the orientation, on the Object Inspector, change the Kind property from sbHorizontal to sbVertical. After adding the a scroll bar, you should set its minimum and maximum values, unless the defaults are fine. Another very important property is the oriental, set as vertical or horizontal using the Kind field. To set the orientation programmatically, assign a TScrollBarKind value to the control:

procedure TForm1.Button1Click(Sender: TObject);
begin
     ScrollBar1.Kind := sbVertical;
end;

When the user is interacting with a a scroll bar by sliding it, the control moves the small square or rectangle, which shows the evolution of the scrolling. The position of the small bar is represented by an integer and its property is called Position. At design time, you can set the initial position the scrollbar would have when the form opens. This is done in the Object Inspector on the Position field. You can also set this value at runtime time. Furthermore, at any time you can find out the position of the scrollbar by calling the Position property. In the following example, the position of the scroll bar displays on a label when the user clicks a button on the form: 

procedure TForm1.Button1Click(Sender: TObject);
begin
     Label1.Caption := IntToStr(ScrollBar1.Position)
end;

Copyright © 2004 FunctionX, Inc.