How to Know if Form is the son of a TPanel is open



  • Opa!
    I have a doubt. How to know if a form assigned as a child of a TPanel Is it fir-tree?
    My form is being created like this:

    if not(Assigned(Form2)) then
    begin
      Form2 := TForm2.Create(self);
      Form2.Parent := Panel1;
      Form2.Show;
    end;
    

    The aim is to avoid creating another instance of the same form. I've tried that, if not(Assigned(Form2)) then and always says she's not Assigned and create another form.

    Source of Unit1

    unit Unit1;
    

    interface

    uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
    Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

    type
    TForm1 = class(TForm)
    Panel: TPanel;
    Button1: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    uses Unit2;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    if not(Assigned(Form2)) then
    begin
    Form2 := TForm2.Create(self);
    Form2.Parent := Panel1;
    Form2.Show;
    end;
    end;

    end.

    Source of Unit2:

    unit Unit2;

    interface

    uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
    Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

    type
    TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form2: TForm2;

    implementation

    {$R *.dfm}

    uses Unit1;

    procedure TForm2.Button1Click(Sender: TObject);
    begin
    Close;
    end;

    end.

    The Source to Unit1 only changes that the Button Opens Unit2
    O FormStle so much Form as Form1 is sealed with fsNormal.



  • Press Alt+P+V delete the form that it includes automatically, something like: Application.CreateForm(TForm2, Form2);

    This logical test:

    if not(Assigned(Form2)) then

    In the documentation explains that Assignedmay not represent the state of existence of the object, if it is = nil.

    The correct is to test whether it is = or different <> nil

    if Form2 = nil then

    Now, what's really wrong is class manipulation, you're using Form2 directly from the class TForm2. It works, but it's not right.

    The correct is that you declare a variable there in { Private declarations } type TForm2 and from this control their existence.

    if vForm = nil then
    begin
      vForm := TForm2.Create(Self);
      vForm .Parent := Panel1;   
    end;
    

    vForm .Show;




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2