[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] | Reference for unit 'Forms' (#lcl)

# TApplication.CreateForm

Creates a Form or component owned by the Application instance.

## Declaration

Source position: forms.pp line 1604

`public procedure TApplication.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

Remark: | 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

TApplication.MainForm |  | Contains the main form for the application.
TApplication.UpdateMainForm |  | Makes the specified form the MainForm in the Application.
TApplication.ProcessMessages |  | Call this method during lengthy operations to ensure the GUI remains responsive.
TCustomForm.FormStyle |  | Indicates the style for the form.
TCustomForm.Show |  | Displays the form instance with support for High DPI scaling.