RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TControlScrollBar.Range Property

Determines how far the scrolling region of the associated control can move.

Pascal
property Range: Integer;
C++
__property int Range;

Range represents the virtual size (in pixels) of the associated control's client area. For example, if the Range of a form's horizontal scroll bar is set to 500, and the width of the form is 200, the scroll bar's Position can vary from 0 to 300. 

If the Range of a horizontal scroll bar is less than the width of the form or scroll box, no horizontal scroll bar appears. If the Range of a vertical scroll bar is less than the height of the form or scroll box, no vertical scroll bar appears. 

The value of Range is set automatically, but can be overridden in the Object Inspector or in program code.  

C++ Examples: 

 

/*
This example starts the form in a position scrolled so that
the right hand edge shows:
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  /* set the width of the form window to display 300 pixels in the client area */
  ClientWidth = 300;
  /* now set the range to twice the client width.
   This means that the form has a logical size
   that is twice as big as the physical window.
   Note that Range must always be at least as big as ClientWidth! */
  HorzScrollBar->Range = 600;
  /* start the form fully scrolled */
  HorzScrollBar->Position = HorzScrollBar->Range - ClientWidth;
  /* clicking the scroll arrows moves the form 10 pixels */
  HorzScrollBar->Increment = 10;
  HorzScrollBar->Visible = true;  // Show the scrollbar
}
/*
This example uses a button and a label on a form and needs
the AutoScroll property set on the form. Place the label
near the left side of the form, and place the button
somewhere near the middle of the form. When the user runs
the application, a horizontal scroll bar does not appear,
because no control on the form is close enough to the right
edge.  Each time the user clicks the button, the button moves
25 pixels to the right, and the calculated Range value is
reported in the caption of the label. Repeatedly clicking the
button eventually moves the button close enough to the edge
of the form (within the Margin amount) so that a horizontal 
scroll bar appears.  Note that the Range property is 
continuously updated as the button moves to the right.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Button1->Left = Button1->Left + 25;
  Label1->Caption = IntToStr(HorzScrollBar->Range);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  HorzScrollBar->Margin = 25;
  HorzScrollBar->Increment = 10;
}
/*
The following example demonstrates form scrolling with the
PgUp and PgDn keys.
*/
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  const int iPageDelta = 10;

  if (Key == VK_NEXT)
    VertScrollBar->Position += iPageDelta;
  else if (Key == VK_PRIOR)
    VertScrollBar->Position -= iPageDelta;
}

 

Delphi Examples: 

{
This example starts the form in a position scrolled so that
the right hand edge shows:
}
procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientWidth := 300;
  with HorzScrollBar do
  begin
    { Set the range to twice the ClientWidth of the form }
    { This means that the form’s logical size is twice as big }
    { as the physical window. }
    { Note that Range must always be larger than the ClientWidth }
    Range := 600; 
    Position := Range - ClientWidth; { start form out fully scrolled }
    Increment := 10;  { clicking the scroll arrows moves the form 10 pixels }
    Visible := True;  { Show the scrollbar }
  end;
end; 

 

{
This example uses a button and a label on a form and needs
the AutoScroll property set on the form. Place the label
near the left side of the form, and place the button
somewhere near the middle of the form. When the user runs
the application, a horizontal scroll bar does not appear,
because no control on the form is close enough to the right
edge.  Each time the user clicks the button, the button moves
25 pixels to the right, and the calculated Range value is
reported in the caption of the label. Repeatedly clicking the
button eventually moves the button close enough to the edge
of the form (within the Margin amount) so that a horizontal 
scroll bar appears.  Note that the Range property is 
continuously updated as the button moves to the right.
}
procedure TForm1.FormCreate(Sender: TObject);
begin
  with HorzScrollBar do
  begin
    Margin:= 25;
    Increment := 10;
    Visible := True;
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Left := Button1.Left + 25;
  Label1.Caption := IntToStr(HorzScrollBar.Range);
end;
{
The following example demonstrates form scrolling with the
PgUp and PgDn keys.
}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
const
  PageDelta = 10;
begin
  With VertScrollbar do
    if Key = VK_NEXT then
      Position := Position + PageDelta
    else if Key = VK_PRIOR then
      Position := Position - PageDelta;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!