Board index » delphi » Delphi 4 and opening up forms or switching to the form

Delphi 4 and opening up forms or switching to the form

I am using Delphi 4.  I can open a form through several ways (ie, File|
New, or a button from another form).

What I would like to know is how to detect if the form is not already
created (and displayed) then to create the form.
(application.createform(Tmyform, myform)  Also, if the form is already
created and displayed, then to simply switch to the form (without
creating the form).

Thanks

--
-----------------------------------------------------------
Patrick Sabourin B.Sc. (Comp. Sci.)
email: patrick_sabou...@technologist.com
web site: http://surf.to/patrick-sabourin

"Even a mosquito doesn't get a slap on the back until
 he starts to work."
                     -Unknown
-----------------------------------------------------------

 

Re:Delphi 4 and opening up forms or switching to the form


Quote
Patrick Sabourin wrote in message <38050C8B.BE83E...@technologist.com>...

>What I would like to know is how to detect if the form is not already
>created (and displayed) then to create the form.
>(application.createform(Tmyform, myform)  Also, if the form is already
>created and displayed, then to simply switch to the form (without
>creating the form).

Whenever you create a form, it is automatically added to a list of forms
managed by the global Screen object (and removed if that form is freed). So
when you want to ensure only a single copy of a particular form is ever
created then just search the current set of forms:

begin
  for i := 0 to Screen.FormCount - 1 do begin
    if Screen.Forms[i] is TMyForm then begin
      Screen.Forms[i].Show;
      Exit;
    end;
  TMyForm.Create(Application).Show;
end;

If you allow this form to be minimized, then you'll want to add some code in
the above to check for that and restore it if required.

--
Wayne Niddery - WinWright Consulting
RADBooks - http://members.home.net/wniddery/
Bill of NO Rights; ARTICLE VIII:  You DON'T have the right to demand that
our children risk their lives in foreign wars to soothe your aching
conscience.

Re:Delphi 4 and opening up forms or switching to the form


Is it possible to create a new unit and place this code in it (with a parameter)
so that it can do this with any form that I would like to have displayed?

If so, what should the parameter be, as every form changes "type" from TForm1 to
TMyMainForm, TMySecondForm, etc...

Thanks

Quote
"Wayne Niddery (TeamB)" wrote:
> Patrick Sabourin wrote in message <38050C8B.BE83E...@technologist.com>...

> >What I would like to know is how to detect if the form is not already
> >created (and displayed) then to create the form.
> >(application.createform(Tmyform, myform)  Also, if the form is already
> >created and displayed, then to simply switch to the form (without
> >creating the form).

> Whenever you create a form, it is automatically added to a list of forms
> managed by the global Screen object (and removed if that form is freed). So
> when you want to ensure only a single copy of a particular form is ever
> created then just search the current set of forms:

> begin
>   for i := 0 to Screen.FormCount - 1 do begin
>     if Screen.Forms[i] is TMyForm then begin
>       Screen.Forms[i].Show;
>       Exit;
>     end;
>   TMyForm.Create(Application).Show;
> end;

> If you allow this form to be minimized, then you'll want to add some code in
> the above to check for that and restore it if required.

> --
> Wayne Niddery - WinWright Consulting
> RADBooks - http://members.home.net/wniddery/
> Bill of NO Rights; ARTICLE VIII:  You DON'T have the right to demand that
> our children risk their lives in foreign wars to soothe your aching
> conscience.

--
-----------------------------------------------------------
Patrick Sabourin B.Sc. (Comp. Sci.)
email: patrick_sabou...@technologist.com
web site: http://surf.to/patrick-sabourin

"Even a mosquito doesn't get a slap on the back until
 he starts to work."
                     -Unknown
-----------------------------------------------------------

Re:Delphi 4 and opening up forms or switching to the form


Quote
> Is it possible to create a new unit and place this code in it

Sure.

Quote
> If so, what should the parameter be, as every form changes
> "type" from TForm1 to TMyMainForm, TMySecondForm,
> etc...

This is specifically why class types exist:

function CreateAndShowForm(AFormClass: TFormClass): TForm;
begin
  for i := 0 to Screen.FormCount - 1 do begin
    if Screen.Forms[i] is AFormClass then begin
      Screen.Forms[i].Show;
      Exit;
    end;
  Result := AFormClass.Create(Application).Show;
end;

- Rick

Other Threads