Board index » delphi » Checking for instance of form

Checking for instance of form

 I have a project with quite a number of forms and rather than create all
the forms at start up I am choosing to create some forms only when and if
they are selected by the user. I am trying to avoid creating more than one
instance of any given form.

Can someone please tell me how to check if there is already an instance of
the form before calling the Create method.

Thanks for any help

Ken

 

Re:Checking for instance of form


Quote
Ken Atkinson wrote in message <6apvta$...@news2.jaring.my>...
>Can someone please tell me how to check if there is already an instance of
>the form before calling the Create method.

Ostensibly, you will create these forms from one "main" form.  If you use a
varible object visible to the entire unit, you can use the Assigned function
to return a boolean about whether it has been assigned something.  Of
course, when you are done with the window, you should probably make sure it
gets freed.  I don't tend to rely on Delphi for that.

Re:Checking for instance of form


If you are trying to write an MDI application this should help.

1. Take the forms you want to create yourself out of 'auto creation' in the
project options.

2.  Call 'CreateForm' for the form if the function/method below returns
nil.

----------------------------------------------------------------------------

function TForm1.FindForm(FrmName: string): TForm;
        var TheForm:TForm;
begin
        TheForm := nil;
        for ix := MDIChildCount - 1 downto 0 do
        begin
                if MDIChildren[ix].Name = FrmName then
                begin
                        TheForm := MDIChildren[ix];
                        break;
                end;
        end;
        Result := TheForm;
end;
--------------------------------------------------------------------------
3. Set the 'Action := caFree' in the Dynamically created form's OnClose
handler

Any syntax errors in the code above are, of course unintentional, ....
but
you get the picture.

Luis Correia.

Ken Atkinson <ke...@pc.jaring.my> wrote in article
<6apvta$...@news2.jaring.my>...

Quote
>  I have a project with quite a number of forms and rather than create all
> the forms at start up I am choosing to create some forms only when and if
> they are selected by the user. I am trying to avoid creating more than
one
> instance of any given form.

> Can someone please tell me how to check if there is already an instance
of
> the form before calling the Create method.

> Thanks for any help

> Ken

Other Threads