Board index » delphi » How to test if a TForm exist.

How to test if a TForm exist.

I am trying to test if a form exists before it is created. The purpose
is to insure only one instance of a form is up at one time. I am finding
the form is not set equal to nil when the close event is executed.
Results from debugging state that the form is in the process of being
destroy yet the form pointer doesn't equal nil.

Code Snippit:

 If (EventForm = nil) then
         EventForm := TEventForm.Create(Self);

This only works the first time through. It will not work after the close
event. The form doesn't equal nil. What is the best test to see if the
form hasn't been closed or is not created.

 

Re:How to test if a TForm exist.


"Bernard W. Durham" <dur...@mrj.com> wrote:

Quote
>I am trying to test if a form exists before it is created.
>Code Snippit:
> If (EventForm = nil) then
>         EventForm := TEventForm.Create(Self);
>This only works the first time through. It will not work after the close
>event. The form doesn't equal nil. What is the best test to see if the
>form hasn't been closed or is not created.

You can use reference to EventForm, for instance
var
  ef: TEventForm;

Inside TEventForm.FormCreate set
ef := EvenForm;
and in FormDestroy set
ef := nil;

then your condition would look:
if not Assigned(ef) then
    EventForm := TEventForm.Create(Self);

Lester

Re:How to test if a TForm exist.


Quote
Bernard W. Durham wrote:

> I am trying to test if a form exists before it is created. The purpose
> is to insure only one instance of a form is up at one time. I am finding
> the form is not set equal to nil when the close event is executed.
> Results from debugging state that the form is in the process of being
> destroy yet the form pointer doesn't equal nil.

> Code Snippit:

>  If (EventForm = nil) then
>          EventForm := TEventForm.Create(Self);

> This only works the first time through. It will not work after the close
> event. The form doesn't equal nil. What is the best test to see if the
> form hasn't been closed or is not created.

The Close method doesn't destroy the form itself.  Use the Destroy (or Free)
method to actually destroy the form.  Then, rather than simply check to see
if the pointer is nil (I'm not sure Delphi sets pointers to nil when Free or
Destroy is called), use the Assigned function.

Code:

    if ( not Assigned( EventForm ) ) then
    begin
        EventForm := TEventForm.Create( Application );
    end;

Hope this helps,
Blake Schwendiman

Re:How to test if a TForm exist.


"Bernard W. Durham" <dur...@mrj.com> wrote:

Quote
>I am trying to test if a form exists before it is created. The purpose
>is to insure only one instance of a form is up at one time. I am finding
>the form is not set equal to nil when the close event is executed.
>Results from debugging state that the form is in the process of being
>destroy yet the form pointer doesn't equal nil.

>Code Snippit:

> If (EventForm = nil) then
>         EventForm := TEventForm.Create(Self);

>This only works the first time through. It will not work after the close
>event. The form doesn't equal nil. What is the best test to see if the
>form hasn't been closed or is not created.

Maybe the problem is that form is hidden and not destroyed. In  OnClose
event there is parameter Action  whichcan be caHide or caFree. See
it - maybe thats the problem

Cheers,
Pawel

Re:How to test if a TForm exist.


In article <4prjuj$...@olorin.nask.pl>, Pawel Ksiezyk
<ksie...@bull.mimuw.edu.pl> writes

Quote
>"Bernard W. Durham" <dur...@mrj.com> wrote:
>>I am trying to test if a form exists before it is created. The purpose
>>is to insure only one instance of a form is up at one time. I am finding
>>the form is not set equal to nil when the close event is executed.
>>Results from debugging state that the form is in the process of being
>>destroy yet the form pointer doesn't equal nil.

>>Code Snippit:

>> If (EventForm = nil) then
>>         EventForm := TEventForm.Create(Self);

>>This only works the first time through. It will not work after the close
>>event. The form doesn't equal nil. What is the best test to see if the
>>form hasn't been closed or is not created.
>Maybe the problem is that form is hidden and not destroyed. In  OnClose
>event there is parameter Action  whichcan be caHide or caFree. See
>it - maybe thats the problem

What you can do IF you know the window was created is to test the
Showing prooperty.  If the form is in a csDestroying state then this
will be False.

HTH.
--
Nicholas Robinson

Re:How to test if a TForm exist.


In article <31C01A65.4...@agdec.com>, Blake Schwendiman
<sch...@agdec.com> writes

Quote
>Bernard W. Durham wrote:

>> I am trying to test if a form exists before it is created. The purpose
>> is to insure only one instance of a form is up at one time. I am finding
>> the form is not set equal to nil when the close event is executed.
>> Results from debugging state that the form is in the process of being
>> destroy yet the form pointer doesn't equal nil.

>> Code Snippit:

>>  If (EventForm = nil) then
>>          EventForm := TEventForm.Create(Self);

CUT -->--

>Code:

>    if ( not Assigned( EventForm ) ) then
>    begin
>        EventForm := TEventForm.Create( Application );
>    end;

This is ok even in MDI environment?  I am writing an MDI app and need to
test if a window is open - I only want one fo the windows to ever be
open.  So when the form closes I use the caFree.  However Assigned with
my form object identifier does not work, perhaps I am doing something
wrong?

--
Nicholas Robinson

Re:How to test if a TForm exist.


In article <e1uzrlA6McwxE...@demon.com>, NRo...@demon.com says...
.>
.>In article <4prjuj$...@olorin.nask.pl>, Pawel Ksiezyk
.><ksie...@bull.mimuw.edu.pl> writes
.>>"Bernard W. Durham" <dur...@mrj.com> wrote:
.>>>I am trying to test if a form exists before it is created. The purpose
.>>>is to insure only one instance of a form is up at one time. I am finding
.>>>the form is not set equal to nil when the close event is executed.
.>>>Results from debugging state that the form is in the process of being
.>>>destroy yet the form pointer doesn't equal nil.
.>>>
.>>>Code Snippit:
.>>>
.>>> If (EventForm = nil) then
.>>>         EventForm := TEventForm.Create(Self);
.>>>
.>>>This only works the first time through. It will not work after the close
.>>>event. The form doesn't equal nil. What is the best test to see if the
.>>>form hasn't been closed or is not created.
.>>Maybe the problem is that form is hidden and not destroyed. In  OnClose
.>>event there is parameter Action  whichcan be caHide or caFree. See
.>>it - maybe thats the problem
.>
.>What you can do IF you know the window was created is to test the
.>Showing prooperty.  If the form is in a csDestroying state then this
.>will be False.
.>
.>HTH.
.>--
.>Nicholas Robinson

You may also find this helpful:

var
  frmAForm : TfrmAForm;
begin
  if (frmAForm = nil) or not(frmAForm.HandleAllocated) then
      frmAForm := TfrmAForm.Create(frmAForm's Owner)
  else frmAForm.SetFocus;
  frmAForm.Show;
  if (frmAForm.WindowState = wsMinimized) then
    frmAForm.WindowState := wsNormal;
end;

Try using release instead of close for the form.  Also, if nothing else,
manually set frmAForm := nil after you have closed
it.

Hope it helps... Chris

Re:How to test if a TForm exist.


In article <4q71ej$...@ren.cei.net>, y...@somehost.somedomain (Chris)
writes:

Quote
>In article <e1uzrlA6McwxE...@demon.com>, NRo...@demon.com says...
>.>
>.>In article <4prjuj$...@olorin.nask.pl>, Pawel Ksiezyk
>.><ksie...@bull.mimuw.edu.pl> writes
>.>>"Bernard W. Durham" <dur...@mrj.com> wrote:
>.>>>I am trying to test if a form exists before it is created. The
purpose
>.>>>is to insure only one instance of a form is up at one time. I am
finding
>.>>>the form is not set equal to nil when the close event is executed.
>.>>>Results from debugging state that the form is in the process of being
>.>>>destroy yet the form pointer doesn't equal nil.
>.>>>
>.>>>Code Snippit:
>.>>>
>.>>> If (EventForm = nil) then
>.>>>         EventForm := TEventForm.Create(Self);
>.>>>
>.>>>This only works the first time through. It will not work after the
close
>.>>>event. The form doesn't equal nil. What is the best test to see if
the
>.>>>form hasn't been closed or is not created.
>.>>Maybe the problem is that form is hidden and not destroyed. In
OnClose
>.>>event there is parameter Action  whichcan be caHide or caFree. See
>.>>it - maybe thats the problem
>.>
>.>What you can do IF you know the window was created is to test the
>.>Showing prooperty.  If the form is in a csDestroying state then this
>.>will be False.
>.>
>.>HTH.
>.>--
>.>Nicholas Robinson

>You may also find this helpful:

>var
>  frmAForm : TfrmAForm;
>begin
>  if (frmAForm = nil) or not(frmAForm.HandleAllocated) then
>      frmAForm := TfrmAForm.Create(frmAForm's Owner)
>  else frmAForm.SetFocus;
>  frmAForm.Show;
>  if (frmAForm.WindowState = wsMinimized) then
>    frmAForm.WindowState := wsNormal;
>end;

>Try using release instead of close for the form.  Also, if nothing else,
>manually set frmAForm := nil after you have closed
>it.

>Hope it helps... Chris

Very long and hard answer to a simple problem.  In the onClose function
for the form, use two lines:
begin
   Action := caFree;
   MyForm := nil;
end;

Frees the memory and sets the memory pointer to nil, and you don't have to
know how the form was closed.

Re:How to test if a TForm exist.


In article <4q71ej$...@ren.cei.net>, Chris <y...@somehost.somedomain>
writes

Quote
>In article <e1uzrlA6McwxE...@demon.com>, NRo...@demon.com says...
>.>
>.>In article <4prjuj$...@olorin.nask.pl>, Pawel Ksiezyk
>.><ksie...@bull.mimuw.edu.pl> writes
>.>>"Bernard W. Durham" <dur...@mrj.com> wrote:
>.>>>I am trying to test if a form exists before it is created. The purpose
>.>>>is to insure only one instance of a form is up at one time. I am finding
>.>>>the form is not set equal to nil when the close event is executed.
>.>>>Results from debugging state that the form is in the process of being
>.>>>destroy yet the form pointer doesn't equal nil.
>.>>>
>.>>>Code Snippit:
>.>>>
>.>>> If (EventForm = nil) then
>.>>>         EventForm := TEventForm.Create(Self);
>.>>>
>.>>>This only works the first time through. It will not work after the close
>.>>>event. The form doesn't equal nil. What is the best test to see if the
>.>>>form hasn't been closed or is not created.
>.>>Maybe the problem is that form is hidden and not destroyed. In  OnClose
>.>>event there is parameter Action  whichcan be caHide or caFree. See
>.>>it - maybe thats the problem
>.>
>.>What you can do IF you know the window was created is to test the
>.>Showing prooperty.  If the form is in a csDestroying state then this
>.>will be False.
>.>
>.>HTH.
>.>--
>.>Nicholas Robinson

>You may also find this helpful:

>var
>  frmAForm : TfrmAForm;
>begin
>  if (frmAForm = nil) or not(frmAForm.HandleAllocated) then
>      frmAForm := TfrmAForm.Create(frmAForm's Owner)
>  else frmAForm.SetFocus;
>  frmAForm.Show;
>  if (frmAForm.WindowState = wsMinimized) then
>    frmAForm.WindowState := wsNormal;
>end;

>Try using release instead of close for the form.  Also, if nothing else,
>manually set frmAForm := nil after you have closed
>it.

i will look at the release bit and the HandleAllocated but setting the
form if it isn't an MDI form is ok, not in MDI though.  Once the window
has been created it exists on its own away from any variable reference
(unless you really need one).

I create my mdi forms like this:

begin
  TMdiForm.Create (Application);
end;

The form then takes control of itself.  

Thanks for the infor anyway...
--
Nicholas Robinson

Re:How to test if a TForm exist.


In article <4q8p2m$...@newsbf02.news.aol.com>, JefSummers
<jefsumm...@aol.com> writes

Quote
>In article <4q71ej$...@ren.cei.net>, y...@somehost.somedomain (Chris)
>writes:

>>In article <e1uzrlA6McwxE...@demon.com>, NRo...@demon.com says...
>>.>
>>.>In article <4prjuj$...@olorin.nask.pl>, Pawel Ksiezyk
>>.><ksie...@bull.mimuw.edu.pl> writes
>>.>>"Bernard W. Durham" <dur...@mrj.com> wrote:
>>.>>>I am trying to test if a form exists before it is created. The
>purpose
>>.>>>is to insure only one instance of a form is up at one time. I am
>finding
>>.>>>the form is not set equal to nil when the close event is executed.
>>.>>>Results from debugging state that the form is in the process of being

>>.>>>destroy yet the form pointer doesn't equal nil.
>>.>>>
>>.>>>Code Snippit:
>>.>>>
>>.>>> If (EventForm = nil) then
>>.>>>         EventForm := TEventForm.Create(Self);
>>.>>>
>>.>>>This only works the first time through. It will not work after the
>close
>>.>>>event. The form doesn't equal nil. What is the best test to see if
>the
>>.>>>form hasn't been closed or is not created.
>>.>>Maybe the problem is that form is hidden and not destroyed. In
>OnClose
>>.>>event there is parameter Action  whichcan be caHide or caFree. See
>>.>>it - maybe thats the problem
>>.>
>>.>What you can do IF you know the window was created is to test the
>>.>Showing prooperty.  If the form is in a csDestroying state then this
>>.>will be False.
>>.>
>>.>HTH.
>>.>--
>>.>Nicholas Robinson

>>You may also find this helpful:

>>var
>>  frmAForm : TfrmAForm;
>>begin
>>  if (frmAForm = nil) or not(frmAForm.HandleAllocated) then
>>      frmAForm := TfrmAForm.Create(frmAForm's Owner)
>>  else frmAForm.SetFocus;
>>  frmAForm.Show;
>>  if (frmAForm.WindowState = wsMinimized) then
>>    frmAForm.WindowState := wsNormal;
>>end;

>>Try using release instead of close for the form.  Also, if nothing else,
>>manually set frmAForm := nil after you have closed
>>it.

>>Hope it helps... Chris

>Very long and hard answer to a simple problem.  In the onClose function
>for the form, use two lines:
>begin
>   Action := caFree;
>   MyForm := nil;
>end;

>Frees the memory and sets the memory pointer to nil, and you don't have to
>know how the form was closed.

Replying to this, I would say that the above will work only if you have
a reference varibale to the form (not always true in MDI).

However, what do you think of this? ...

begin
  Action := caFree;
  TForm (Self) := nil;
end;

I haven't got Delphi on this machine so cannot test it.  This
assignation will work but how will the form actually perform?
--
Nicholas Robinson

Re:How to test if a TForm exist.


On Thu, 13 Jun 1996 07:40:53 -0600, Blake Schwendiman <sch...@agdec.com>
wrote:

:The Close method doesn't destroy the form itself.  Use the Destroy (or Free)
:method to actually destroy the form.  Then, rather than simply check to see

Doesn't setting Action := caFree in the OnClose event free the resources used
by the form without having to call Free explicitly?

-----
Kerry Sanders
code...@mindspring.com
http://www.mindspring.com/~ksanders

Re:How to test if a TForm exist.


Quote
> code...@mindspring.com (Kerry Sanders) wrote in article

<31cb68c3.2946...@news.mindspring.com>...

Quote
> On Thu, 13 Jun 1996 07:40:53 -0600, Blake Schwendiman <sch...@agdec.com>
> wrote:

> :The Close method doesn't destroy the form itself.  Use the Destroy (or
Free)
> :method to actually destroy the form.  Then, rather than simply check to
see

> Doesn't setting Action := caFree in the OnClose event free the resources
used
> by the form without having to call Free explicitly?

I would think so, but if so, according to the docs, you can't hurt
anything by
calling Free again.  Probably is a good idea to set Form := nil afterwards
also.

Other Threads