zac
zac

Reputation: 4898

Display a form in two different ways

I display form in two different ways. The first one as a child of a tab sheet of page control like this:

myform := Tmyform.Create(<tab sheet of page control>);
myform.Parent := <tab sheet of page control>;
myform.Align := alClient;
myform.BorderStyle := bsNone;

myform.Visible := true;

This tab is always visible.

The second time I display it as a normal form like this:

myform := Tmyform.Create(nil);
myform.ShowModal;

I do this because I need to display the same form with some visual controls visible and in second time hidden.

My problem I noticed now the application consume more memory each time I open the form using the second method and if I used action := cafree in the close event I get access violation when I try to shutdown the application while the tab sheet is opened so how I should fix this without using two forms with same controls ?.

I use Delphi XE5

Upvotes: 1

Views: 1081

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595896

TForm was never intended or designed to be embedded in other controls. Use TFrame instead, it was specifically designed for this purpose.

var
  frame: TMyFrame;
begin
  frame := TMyFrame.Create(TheTabSheet);
  frame.Parent := TheTabSheet;
  frame.Align := alClient;
  frame.Visible := true;
end;

var
  form: TForm;
  frame: TMyFrame;
begin
  form := TForm.CreateNew(nil); // yes, a blank TForm
  try
    form.Width := ...;
    form.Height := ...;
    form.Caption := ...;
    frame := TMyFrame.Create(form);
    frame.Parent := form;
    frame.Align := alClient;
    frame.Visible := true;
    form.ShowModal;
  finally
    form.Free;
  end;
end;

Upvotes: 3

Related Questions