BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How Can I Find Out if already Exists

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi IDE
View previous topic :: View next topic  
Author Message
Juan Rosique
Guest





PostPosted: Fri Sep 12, 2003 9:50 pm    Post subject: How Can I Find Out if already Exists Reply with quote



Newbie question, or rather, I just don't remember. I know I have done in
D2, but for the life of I can't find the darn source.

How one goes about finding out if an instance of an object, say, a form,
exists already? Is it with Assigned( )?

Any input?

Thanks for your time


Back to top
Kurt Barthelmess
Guest





PostPosted: Fri Sep 12, 2003 10:18 pm    Post subject: Re: How Can I Find Out if already Exists Reply with quote



"Juan Rosique" <jrosique (AT) pmonarch (DOT) com> wrote:

Quote:
Newbie question, or rather, I just don't remember. I know I have done in
D2, but for the life of I can't find the darn source.

How one goes about finding out if an instance of an object, say, a form,
exists already? Is it with Assigned( )?

If Assigned(X) returns True if X <> nil. But that is not adequate to
assure that X is valid (as opposed to nil. Simple Example:

var X: TObject;
...
X := TObject.Create;
X.Free;

While X is no longer a valid reference to an object, Assigned(X) is
True. You can try to avoid this by always setting "X" to nil before
you Free it, but other things could cause X to be free'd as well. So
be careful.

Good luck.

Kurt



Back to top
Kurt Barthelmess
Guest





PostPosted: Sat Sep 13, 2003 10:59 am    Post subject: Re: How Can I Find Out if already Exists Reply with quote



"Juan Rosique" <jrosique (AT) pmonarch (DOT) com> wrote:

Quote:
Thanks Kurt, But I guess I did not lay my question correctly. What I am
trying to accomplish is to find out if a form is already instantiated so I
do not create it again.

My existing code:
procedure ShowOwnerForm (OnWhich: TWinControl);
var
OwnerForm : TOwnerForm;
begin
OwnerForm := TOwnerForm.Create(Application);
OwnerForm.Parent := OnWhich;
OwnerForm.Align := alClient;
OwnerForm.Show;
end;

There are a couple of ways to approach this. You can walk through
Screen.Forms:

with Screen do
for I := 0 to FormCount - 1 do
if Forms[I] is TOwnerForm then
ShowMessage('found');

You can walk through Application.Components:
with Application do
for I := 0 to ComponentCount - 1 do
if Components[I] is TOwnerForm then
ShowMessage('found');

You could remove the local declaration for OwnerForm and use the
OwnerForm declared in the unit for TOwnerForm. This requires the
OwnerForm unit to do a bit of cleanup for itself in the OnDestroy
event handler:

procedure TOwnerForm.FormDestroy(Sender: TObject);
begin
OwnerForm := nil;
end;
:
Then to do the test, use:

procedure ShowOwnerForm (OnWhich: TWinControl);
begin
if Assigned(OwnerForm) then
begin
OwnerForm.Show;
Exit;
end;
OwnerForm := TOwnerForm.Create(Application);
OwnerForm.Parent := OnWhich;
OwnerForm.Align := alClient;
OwnerForm.Show;
end;

Quote:
The problem is that this piece of code resides in a unit outside the
application's main form. When I put (SELF) instead of Application, it won't
compile, arguing that "Self" is not found.

Since this is a stand-alone precedure, not a method of the main form
or any other component, "Self" is not defined. Unless you have a
reason for making something else the Owner, Application is a good
Owner anyhow. But you wanted the main form to be the Owner, you could
have written:

OwnerForm := TOwnerForm.Create(Application.MainForm);

or if the main form is named TForm1 in Unit1, add Unit1 to your "uses"
clause and write:

OwnerForm := TOwnerForm.Create(Form1);

Quote:
All I want to do is to check if the
form is already there

I think one of the ideas above should deal with that.

Quote:
and do two things (a) populate my main form "Window"
menu with the name of the OwnerForm

This gets a bit trickier. Try something like (assuming you are
creating the form and not bailing out early, and that Form1 in Unit1
is the main form) this afetr you actually create OwnerForm:

var
MenuItem : TMenuItem;
....
MenuItem := TMenuItem.Create(Form1);
MenuItem.Caption := 'abc';
MenuItem.OnClick := Form1.OwnerFormClick;
Form1.Window1.Add(MenuItem);

OwnerClick is the method in Form1 to handle the menu item selection
for OwnerForm. Window1 is the "window" menu item on Form1's main menu.

That should give you some ideas. Followup here if you have problems or
questions.

Good luck.

Kurt



Back to top
Juan Rosique
Guest





PostPosted: Sun Sep 14, 2003 12:59 am    Post subject: Re: How Can I Find Out if already Exists Reply with quote

Thanks Kurt!

I will work with this over the weekend and will let you know the results.

Thank you thank you
"Kurt Barthelmess (TeamB)" <kbarthelmess (AT) compuserve (DOT) com> wrote

Quote:
"Juan Rosique" <jrosique (AT) pmonarch (DOT) com> wrote:

Thanks Kurt, But I guess I did not lay my question correctly. What I am
trying to accomplish is to find out if a form is already instantiated so
I
do not create it again.

My existing code:
procedure ShowOwnerForm (OnWhich: TWinControl);
var
OwnerForm : TOwnerForm;
begin
OwnerForm := TOwnerForm.Create(Application);
OwnerForm.Parent := OnWhich;
OwnerForm.Align := alClient;
OwnerForm.Show;
end;

There are a couple of ways to approach this. You can walk through
Screen.Forms:

with Screen do
for I := 0 to FormCount - 1 do
if Forms[I] is TOwnerForm then
ShowMessage('found');

You can walk through Application.Components:
with Application do
for I := 0 to ComponentCount - 1 do
if Components[I] is TOwnerForm then
ShowMessage('found');

You could remove the local declaration for OwnerForm and use the
OwnerForm declared in the unit for TOwnerForm. This requires the
OwnerForm unit to do a bit of cleanup for itself in the OnDestroy
event handler:

procedure TOwnerForm.FormDestroy(Sender: TObject);
begin
OwnerForm := nil;
end;
:
Then to do the test, use:

procedure ShowOwnerForm (OnWhich: TWinControl);
begin
if Assigned(OwnerForm) then
begin
OwnerForm.Show;
Exit;
end;
OwnerForm := TOwnerForm.Create(Application);
OwnerForm.Parent := OnWhich;
OwnerForm.Align := alClient;
OwnerForm.Show;
end;

The problem is that this piece of code resides in a unit outside the
application's main form. When I put (SELF) instead of Application, it
won't
compile, arguing that "Self" is not found.

Since this is a stand-alone precedure, not a method of the main form
or any other component, "Self" is not defined. Unless you have a
reason for making something else the Owner, Application is a good
Owner anyhow. But you wanted the main form to be the Owner, you could
have written:

OwnerForm := TOwnerForm.Create(Application.MainForm);

or if the main form is named TForm1 in Unit1, add Unit1 to your "uses"
clause and write:

OwnerForm := TOwnerForm.Create(Form1);

All I want to do is to check if the
form is already there

I think one of the ideas above should deal with that.

and do two things (a) populate my main form "Window"
menu with the name of the OwnerForm

This gets a bit trickier. Try something like (assuming you are
creating the form and not bailing out early, and that Form1 in Unit1
is the main form) this afetr you actually create OwnerForm:

var
MenuItem : TMenuItem;
...
MenuItem := TMenuItem.Create(Form1);
MenuItem.Caption := 'abc';
MenuItem.OnClick := Form1.OwnerFormClick;
Form1.Window1.Add(MenuItem);

OwnerClick is the method in Form1 to handle the menu item selection
for OwnerForm. Window1 is the "window" menu item on Form1's main menu.

That should give you some ideas. Followup here if you have problems or
questions.

Good luck.

Kurt




Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi IDE All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.