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 

Different ways to create a form

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Using
View previous topic :: View next topic  
Author Message
Reid Roman
Guest





PostPosted: Thu Jul 24, 2003 7:37 pm    Post subject: Different ways to create a form Reply with quote



There are different ways to create a TForm. Which is best?

Application.CreateForm(TMyForm, frmMyForm);
frmMyForm := TMyForm.Create(nil);
frmMyForm := TMyFrom.Create(Application);

If you are creating forms at runtime, which is the best method to use?

Thanks,

--
Reid Roman
Future Generation Software
http://www.fgsoft.com


Back to top
Dell Stinnett
Guest





PostPosted: Thu Jul 24, 2003 7:54 pm    Post subject: Re: Different ways to create a form Reply with quote



Quote:
Application.CreateForm(TMyForm, frmMyForm);
Occurs in the .dpr file - form is autocreated by the project. Don't use for

creating forms at runtime - all autocreate forms will be created when the
program starts.

Quote:
frmMyForm := TMyForm.Create(nil);
Form is created in code and has no parent. When the application closes,

there's no guarantee that the form will be "freed" so that memory cleanup
takes place.

Quote:
frmMyForm := TMyFrom.Create(Application);
Form is created in code and the application is the parent. This is a better

option than the one above because it will be freed.

I usually have two ways of creating forms in code. If I'm creating a form
that will be shown with ".Show", I use the last option above. If I'm
creating a form that will be shown with ".ShowModal", I use a "class
function" of the form to create it, set any required variables, show it, and
gather results before freeing it. It usually looks something like this:

- In the Interface section, Public declaration for the form:
class function Execute(var SomeParam: String): Boolean;

- In the Implementation Section:
class function TMyForm.Execute(var SomeParam: String): Boolean;
begin
result := false;
With TMyForm.Create(Application) do
Try
SomeValue := SomeParam;
If ShowModal = mrOK then
begin
SomeParam := SomeNewValue;
result := True;
end;
finally
free;
end;
end;

- When it's used in another unit:
If TMyForm.Execute(StringVal) then
//do something with StringVal

You don't have to have an active instance of the form to use a class
function or a class procedure because each is assumed to have its own
instance as part of its functionality.

-Dell



Back to top
Jeremy Chapman
Guest





PostPosted: Thu Jul 24, 2003 8:11 pm    Post subject: Re: Different ways to create a form Reply with quote



Also, you don't need to pass Application to the create method of a form, you
can pass another form, which will make sence if its a sub form of a form.


"Peter Thörnqvist" <peter3 (AT) no (DOT) spam.peter3.com> wrote

Quote:
frmMyForm := TMyForm.Create(nil);
Form is created in code and has no parent.
frmMyForm := TMyFrom.Create(Application);
Form is created in code and the application is the parent.
Should be *Owner*, not parent

--
Regards,

Peter Thornqvist
(JVCL Coordinator)
http://jvcl.sourceforge.net



Back to top
Reid Roman
Guest





PostPosted: Thu Jul 24, 2003 8:51 pm    Post subject: Re: Different ways to create a form Reply with quote


"Reid Roman" <reidr (AT) fgsoft (DOT) com> wrote


Thank You all!


--
Reid Roman
Future Generation Software
http://www.fgsoft.com

Disclaimer:
------------------------------
Filtering now in effect. If I
don't reply, it is because I
don't see it.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Using 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.