A. * The following code will create a modal password form at runtime. The TPasswordForm type is a TForm descendent class defined either in the current unit or in a separate unit referenced by the current unit's uses clause.
with TPasswordForm.Create(Application) do
begin
( i.e TForm1, TPasswordForm etc. }
ShowModal;
{ Display form as a modal window }
Free;
{ Free the form when it is closed }
end;
* The following are the general steps to add a component to a form at run-time:
1. Declare an instance variable of the component
type that you wish to create {i.e. TButton }. Note: instance variables
are used to point to an actual instance of an object. They are not objects
themselves.
2. Use the component's Create constructor
to create an instance of the component and assign the instance to the instance
variable declared in step 1. All components' Create constructors
take a parameter - the component's owner. Except in special
circumstances, you should always pass the form as the owner parameter
of the component's Create constructor.
3. Assign a parent to the component's Parent
property (i.e. Form1, Panel1, etc). The Parent determines where the
component will be displayed, and how the component's Top and Left
coordinates are interpreted. To place a component in a groupbox,
set the component's Parent property to the groupbox. For a
component to be visible, it must have a parent to display itself within.
4. Set any other properties that are necessary
(i.e. Width, Height).
5. Finally, make the component appear on the
form by setting the component's Visible property to True.
6. If you created the component with an owner,
you don't need to do anything to free the component - it will be freed
when the owner is destroyed. If you did not give the component an
owner when you created it, you are responsible for making sure the component
is freed when it is no longer needed.
The following demonstrates how to add a TButton component to the current form at run-time:
var
TempButton : TButton; {
This is only a pointer to a TButton }
begin
TempButton := TButton.Create(Self);
{ Self refers to the form }
TempButton.Parent := Self;
{ Must assign the Parent }
TempButton.Caption := 'Run-time';
{ Assign properties now }
TempButton.Visible := True;
{ Show to button }
end;
Since the button was created with an owner,
it will be freed automatically when its owner, the form, is freed.