How can I display a Delphi form in a panel?

Friso picture Friso · Feb 23, 2015 · Viewed 7.8k times · Source

I've tried to follow the example of http://docwiki.embarcadero.com/CodeExamples/XE7/en/FMXEmbeddedForm_(Delphi) but I hit my first problem with the children of TCustomForm, which are apparently read only, so I commented that out and put in ArgForm.Parent:= ArgParent; instead, but I still just get an empty screen and can't see the buttons that are in my second form.

The code for my main form is:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure EmbedForm(ArgParent : TControl; ArgForm : TCustomForm);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form2:= TForm2.Create(Self);
  EmbedForm(Panel1, Form2);
end;

procedure TForm1.EmbedForm(ArgParent: TControl; ArgForm: TCustomForm);
begin
  //while ArgForm.ChildrenCount>0 do
  //begin
    //ArgForm.Children[0]:= ArgParent);
  //end;
  ArgForm.Parent:= ArgParent;
end;
end.

The code for the form to put in the panel of my main form is:

unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

end.

Answer

David Heffernan picture David Heffernan · Feb 23, 2015

You just need to use the code exactly as it is written in the example. You wrote:

while ArgForm.ChildrenCount>0 do
  ArgForm.Children[0]:= ArgParent;

But the code in the example you linked to reads:

while ArgForm.ChildrenCount>0 do
  ArgForm.Children[0].Parent := ArgParent;

On the plus side, you have at least corrected the spelling of embed.