Delphi - Dynamic Components

There are times that you want to modify a form design at runtime. There are several possibilities

Creating a Button | Linking to a Component on Another Form
Linking Tabsheets from Another Form | TPersistent.Assign


Creating a Button

This code demonstrates one way to create a windowed component at run time. For a TButton, Height, Width, and Visible are automatically set to the standard default values.


Linking to a Component on Another Form

In some cases, I know what I want and where it goes. In that case, I design a form with those components and then link them at runtime.


Linking Tabsheets from Another Form

This technique is useful for displaying Tabsheets and Panels because all the components displayed on them will be displayed in the main form.

In this example, a tabbed control was created on each of 2 pages. Only one page was displayed, the other stayed hidden. When the button was clicked, the tab sheet on the hidden page was displayed on the main page.

Notice that for Tabsheets, the parent should be a PageControl ... not the Form.

This has the advantage that a single line of code causes Form1 to display all the components on Form2.TabSheet1 ... even the underlying code is executed when a button on Form2.TabSheet1 in clicked.


TPersistent.Assign

I tried using TPersistent.Assign to copy a component from one form to another, but it does not work. This is from the Delphi 5 TPersistent.Assign help
In general, the statement “Destination := Source” is not the same as the statement “Destination.Assign(Source)”. The statement “Destination := Source” makes Destination reference the same object as Source, whereas "Destination.Assign(Source)" copies the contents of the object referenced by Source into the object referenced by Destination.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Delphi / Dynamic_Components.html