created on the fly

Q.   "How can VCL components be created on the fly at run-time?"

A.   * The following code will create a modal password form at runtime.
     The TPasswordForm type is assumed to be created already in a
     seperate unit.

 with TPasswordForm.Create(Application) do
 begin  ( i.e TForm1, TPasswordForm etc. }
   ShowModal;
   Free;
 end;

     * The following are the general steps to add a component to a form at
     run-time:

     1. Create 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 method to create an instance
        of the component and assign the instance reference to the instance
        variable created in step 1.
     3. Assign a parent to the component's Parent property (i.e. Form1,
        Panel1, etc)
        property.
     4. Set any other properties that are necessary  (i.e. Width, Height).
     5. Finally, to make the component appear on the form by setting the
        component's Visible property to True.
     6. When done with the component make sure the component's Free method
        is called.

     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;

     Note: The TempButton's Free method must be called before or during
     the time that the form gets closed.

     * Creating components using RTTI (Run time Type Information)

     With Delphi's run time type information (RTTI), an object can easily
     create another instance of itself by calling the NewInstance method
     or using it's ClassType method which returns it's class and then using
     that to call the Create constructor:

     NewObject := ExistingObject.ClassType.Create(...) ;

     The DynaInst project in the \Delphi\Demos\DynaInst directory
     demonstrates how to create controls at runtime using RTTI.