Link to home
Start Free TrialLog in
Avatar of ka1a
ka1a

asked on

Only one form of each type

How can I find out of a form already exists?  I have a form that is created at runtime (click on a button => 'TfrmExample.create(Self)').  When the button is clicked, but the form already exists, there may not be created a second one.  Only one form of each type is allowed?  How can I control this.  It may not be done with MDIforms and MDIchild forms.  Al the forms are 'fsNormal'.

Dirk.

PS sorry for my poor English.
Avatar of inthe
inthe

hi,
check if the form variable you used to create form is assigned
ie:

var
someform : tform;

---------- later on ---

if assigned(someform) then
//form exists already dont create
else
//form dont exist yet so can be created


(note:
remember to free the form when finished before using it again otherwise someform will still be assigned)
Avatar of ka1a

ASKER

hi inthe,

I tried with assigned.  It seems that it don't work.  I get always the message the form already exists.  The code folows below:

procedure TfrmMain.SpeedButton1Click(Sender: TObject);
var LlnFiche:TfrmLlnFiche;
begin
  if assigned(LlnFiche) then
    caption:='Already exists' else
  begin
    LlnFiche:=TfrmLlnFiche.Create(Self);
    LlnFiche.Create(Self);
  end;
end;

What am I doing wrong?

Thanks in advance.

Dirk.

PS sorry for my poor English.
hi,
variable must be global ,here an example:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

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

var
  Form1: TForm1;
  form2 : tform;

  implementation

{$R *.DFM}

uses unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
if assigned(form2) then
form1.caption := 'form2 already exists'
else begin
form2 := tform2.Create(self);
form2.show;
end;
end;

end.









unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

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

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

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

end.
Avatar of ka1a

ASKER

Last question:

It seems to work.  But when I close form 2 and then try to create it back by clicking the button on form 1, I get the message that form 2 already exists.  I think that I need to free the variable form2 when form2 is closed.  How can I do that and where to I put the code.  I tried to do 'form1.form2.free' in the onclose event of form2 but then I get an error message.

Thanks again for al your help.

Dirk.
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ka1a

ASKER

Thanks Inthe,

I learned a lot.  It works great.  You where a great help.

Thanks again.

Dirk.