How to Know if the Form that is the son of a Tpanel is open

Asked

Viewed 138 times

3

Opa!
I have a question. How to know if a form assigned as a child of a TPanel this fir?
My form is being created like this:

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

The intention is to avoid Creating another instance of the same form. I’ve tried this, if not(Assigned(Form2)) then and always says she’s not Assigned and creates another form.

Source of the 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 the 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 Unit1 Source only changes that the Button of the opening to Unit2
The FormStle so much of Form2 as Form1 is set with fsNormal.

  • You can show the class creation TForm2 ?

  • The creation is standard of Delphi not modified anything

  • Form2 is a public or local variable?

  • Old I went in New - VCL Application - Delphi which generated Unit1. Then I went to New - Vcl Form - Delphi which generated the Unit2, I put the two Buttons do not create anything Len of the two Processes. all there was standard of Delphi.

1 answer

2


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

This logical test:

if not(Assigned(Form2)) then

In the documentation it 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 the class manipulation, you’re using Form2 directly from the class TForm2. It works, but it’s not right.

The correct is for you to declare a variable there in the { Private declarations } of the kind TForm2 and from this control their existence.

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

vForm .Show;
  • I will declare Tform2 in the Unit1 Private?

  • In unit1 private you declare vForm: TForm2;

  • Then for each Form you will have a Variable?

  • 1

    @It has to be.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.