Board index » delphi » Please Help: Form Instance (Not App Instance)

Please Help: Form Instance (Not App Instance)

I was just wondering if you could help me with a beginner question? The
question is:
How to avoid multiple instances of a form (not application). I've tried to
find answers on the net, but I couldn't find it.

So for example on a button click even:

 procedure TForm1.Button1Click (Sender: T Object);

 Begin

  Case FormAlreadyCreated of  <<< --- is there a function to check whether a
form is already created?
    True:  Form2.setfocus;

    False: {Create Form Instead}
     Begin
         Form2:=TForm2.Create(Self);
                       Form2.show;
     end;
  end;
 end;

There are some components available to check multiple instances of an
application but not form creation like i just mentioned above. Can somebody
help me? *response through e-mail will be greatly appreciated*

I'm sorry for the troubles. Thanks a lot!

Regards,
Indra
aft...@hotkey.net.au

 

Re:Please Help: Form Instance (Not App Instance)


The "normal" method is to make sure that the form variable, Form2 in your
example, is set to nil until it is created. Also, when closing/destroying
the form, set it to nil again. That way you can check the variable with
Assigned.

Woody

Quote
Indra Setiawan <aft...@hotkey.net.au> wrote in message

news:xN7G3.146$kv3.13212@nsw.nnrp.telstra.net...
Quote
> I was just wondering if you could help me with a beginner question? The
> question is:
> How to avoid multiple instances of a form (not application). I've tried to
> find answers on the net, but I couldn't find it.

> So for example on a button click even:

>  procedure TForm1.Button1Click (Sender: T Object);

>  Begin

>   Case FormAlreadyCreated of  <<< --- is there a function to check whether
a
> form is already created?
>     True:  Form2.setfocus;

>     False: {Create Form Instead}
>      Begin
>          Form2:=TForm2.Create(Self);
>                        Form2.show;
>      end;
>   end;
>  end;

> There are some components available to check multiple instances of an
> application but not form creation like i just mentioned above. Can
somebody
> help me? *response through e-mail will be greatly appreciated*

> I'm sorry for the troubles. Thanks a lot!

> Regards,
> Indra
> aft...@hotkey.net.au

Re:Please Help: Form Instance (Not App Instance)


You can check if the form already exists with

  If Application.FindComponent('frmNewMotions') = nil
    then Application.CreateForm(TfrmNewMotions, frmNewMotions);
  frmNewMotions.ShowModal;
  If not FormIsPersistent('frmNewMotions') then frmNewMotions.Free;

FormIsPersistent() is unimportant here (own function). You can use this
method to save RAM by only creating seldomly used forms if you actually
need them.

hannes

Other Threads