Sign in Join
FPDoc Onlineexperiment

TApplication.CreateForm procedure

Member of TApplicationpublicIn unit Forms

Creates a Form or component owned by the Application instance.

Declaration

pascal
procedure CreateForm(InstanceClass: TComponentClass; out Reference)

Arguments

InstanceClass

The class type used to create the new class instance.

Reference

The output parameter used to return the new component reference.

Description

The method name is slightly misleading, and kept only for Delphi compatibility. The method can actually create any kind of component.

CreateForm creates a new Component instance of the given class, and sets the pointer to the component variable in Reference. If InstanceClass is a TForm descendant, it will be added to the list of forms in the application. It is assigned as the MainForm in Application if a form has not already been assigned to the property. A form with its FormStyle property set to fsMDIChild or fsSplash is not used as the MainForm for the Application.

If the new form instance is a splash form (FormStyle=fsSplash), it is displayed immediately and a message processing loop is started for the form instance.

Use in a .lpr Project File

The most common use of CreateForm is in the project (.lpr) program file. It is used to initialize the auto-created form instances for the project. For example:

program PoFileMaintenance;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  {$IFDEF HASAMIGA}
  athreads,
  {$ENDIF}
  Interfaces, Forms, PotFile, Unit1, 
  { you can add units after this };

{$R *.res}

begin
  RequireDerivedFormResource := True;
  Application.Scaled := True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Use in Program Code

Another common use of CreateForm is to allocate a dynamic, temporary form instance as needed in program code. For example:

function ShowDialog(FormClass: TFormClass): Boolean;
var
  Dlg: TForm;
begin
  Application.CreateForm(FormClass, Dlg);
  try
    Result := Dlg.ShowModal in [mrOk, mrYes];
  finally
    Dlg.Free;
  end;
end;

See also

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.