RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TControl.SetBounds Method

Sets the Left, Top, Width, and Height properties all at once.

Pascal
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); virtual;
C++
virtual __fastcall SetBounds(int ALeft, int ATop, int AWidth, int AHeight);

Use SetBounds to change all of the component's boundary properties at one time. The same effect can be achieved by setting the Left, Top, Width, and Height properties separately, but SetBounds changes all four properties at once ensuring that the control will not repaint between changes. 

Specify the values for the Left, Top, Width, and Height properties as the value of the ALeft, ATop, AWidth, and AHeight parameters, respectively. 

Calling SetBounds does not necessarily result in the Left, Top, Width, and Height properties changing to the specified values. Before the properties are changed, the AutoSize or Constraints property may limit the changes, and an OnCanResize (or OnConstrainedResize) event handler may change the new values. After the control's Left, Top, Width, and Height properties are changed, SetBounds generates an OnResize event.

Note: Component writers can change the Left, Top, Width, and Height properties while bypassing all resize events and constraint or autosize logic by using the UpdateBoundsRect method instead.
 

C++ Examples: 

 

//
//This example demostrates the usage of Margins and AlignWithMargins
//properties. Note that Margins will only work when AlignWithMargins
//is set to true and Align is not alNone.
//

#define TopPanelMargin 10
#define BottomPanelMargin 20

TPanel *TopPanel, *BottomPanel;

void __fastcall TForm2::FormCreate(TObject *Sender)
{
    /* Create a new panel and align it to top */
    TopPanel = new TPanel(this);
    TopPanel->Parent = this;
    TopPanel->Align = alTop;
    TopPanel->Height = (ClientHeight / 2);

    /* Create a new panel and align it to the remaining client size */
    BottomPanel = new TPanel(this);
    BottomPanel->Parent = this;
    BottomPanel->Align = alClient;

    /* Set Top panel margins */
    /* Note: Margins will not have an effect if Align is alNone */
    TopPanel->Margins->SetBounds(TopPanelMargin, TopPanelMargin,
                                 TopPanelMargin, TopPanelMargin);
    TopPanel->AlignWithMargins = true;
    TopPanel->Caption = "TopPanel";

    /* Set Top Bottom margins */
    /* Note: Margins will not have an effect if Align is alNone */
    BottomPanel->Margins->SetBounds(BottomPanelMargin, BottomPanelMargin,
                                   BottomPanelMargin, BottomPanelMargin);
    BottomPanel->AlignWithMargins = true;
    BottomPanel->Caption = "BottomPanel";

    if ((TopPanel->Top != TopPanelMargin) ||
       (TopPanel->Left != TopPanelMargin) ||
       (TopPanel->Width != (ClientWidth - (TopPanelMargin * 2))) ||
       (BottomPanel->Top != (TopPanel->Height + (TopPanelMargin * 2) +
                                               BottomPanelMargin)) ||
       (BottomPanel->Left != BottomPanelMargin) ||
       (BottomPanel->Width != (ClientWidth - (BottomPanelMargin * 2))))
       {
          MessageDlg(AnsiString(
            "This should not happen! Position and size are ")+
            "calculated relative to the margins",
            mtError, TMsgDlgButtons() << mbOK, 0);
       }
}

 

Delphi Examples: 

{
This example demostrates the usage of Margins and AlignWithMargins
properties. Note that Margins will only work when AlignWithMargins
is set to true and Align is not alNone.
}
const
  TopPanelMargin = 10;
  BottomPanelMargin = 30;

var
  TopPanel, BottomPanel : TPanel;

procedure TForm2.FormCreate(Sender: TObject);
begin
  { Create a new panel and align it to top }
  TopPanel := TPanel.Create(Self);
  TopPanel.Parent := Self;
  TopPanel.Align := alTop;
  TopPanel.Height := (ClientHeight div 2);

  { Create a new panel and align it to the remaining client size }
  BottomPanel := TPanel.Create(Self);
  BottomPanel.Parent := Self;
  BottomPanel.Align := alClient;

  { Set Top panel margins }
  { Note: Margins will not have an effect if Align is alNone }
  TopPanel.Margins.SetBounds(TopPanelMargin, TopPanelMargin,
                             TopPanelMargin, TopPanelMargin);
  TopPanel.AlignWithMargins := true;
  TopPanel.Caption:= 'TopPanel';

  { Set Bottom panel margins }
  { Note: Margins will not have an effect if Align is alNone }
  BottomPanel.Margins.SetBounds(BottomPanelMargin, BottomPanelMargin,
                               BottomPanelMargin, BottomPanelMargin);
  BottomPanel.AlignWithMargins := true;
  BottomPanel.Caption:= 'BottomPanel';

  if (TopPanel.Top <> TopPanelMargin) or
     (TopPanel.Left <> TopPanelMargin) or
     (TopPanel.Width <> (ClientWidth - (TopPanelMargin * 2))) or
     (BottomPanel.Top <> (TopPanel.Height + (TopPanelMargin * 2) +
                                               BottomPanelMargin)) or
     (BottomPanel.Left <> BottomPanelMargin) or
     (BottomPanel.Width <> (ClientWidth - (BottomPanelMargin * 2)))
     then
       MessageDlg('This should not happen! Position and size are ' +
            'calculated relative to the margins', mtError, [mbOK], 0);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!