chami.com/tips/
Last  Home  Next
 Internet
 Programming
 Windows


Click for details
Keywords
Components
Delphi 1.x
Delphi 2.x
Delphi 3.x
Delphi
Source Code

Downloads
runtime.pas
runtime2.pas

How to add components to forms at run time

    See Also
  Easy way to find out which properties should be set when creating components at run time
  Take another look at your form...

It's easy and fun to create setup components at design time, but if you must, it's not difficult to create them at run time as well.

For example, let's say you want to create a "TLabel" object called "Label1" with the caption "hello, world!" at run time and place it on your form at (50, 60) x and y coordinates:

//
// assuming: your form is called "Form1"
//
with TLabel.Create( Form1 ) do
begin
  Parent  := Form1;  // this is important
  Left    := 50;     // X coordinate
  Top     := 60;     // Y coordinate
  Caption := 'hello, world';

  //
  // set your other parameters here...
  //

  //
  // you don't have to set the Name
  // parameter, but...
  //
  Name    := 'Label1';

  //
  // finally make it visible
  //
  Visible := True;
end;
Listing #1 : Delphi code. Right click runtime.pas to download.

The same example, this time using a variable to keep track of the "TLabel" component:

var
  l : TLabel;

begin
  //
  // assuming: your form is called "Form1"
  //
  l := TLabel.Create( Form1 );

  l.Parent  := Form1;  // this is important
  l.Left    := 50;     // X coordinate
  l.Top     := 60;     // Y coordinate
  l.Caption := 'hello, world';

  //
  // set your other parameters here...
  //

  //
  // you don't have to set the Name
  // parameter, but...
  //
  l.Name    := 'Label1';

  //
  // finally make it visible
  //
  l.Visible := True;
end;
Listing #2 : Delphi code. Right click runtime2.pas to download.

 
Related Links Email Print 
Created on 19-Nov-1996. Source code colorized using CodeColorizer.
Copyright (C) 1996-99 Chami.com All Rights Reserved. Reproduction in whole or in part
or in any form or medium without express written permission of Chami.com is prohibited.
Information on this page is provided as-is without warranty of any kind. Use at your own risk.
Free Downloads | Products & Services | Privacy Statement | Terms & Conditions | Advertising Info