Board index » delphi » Different ways to create a form

Different ways to create a form


2003-07-25 03:37:17 AM
delphi190
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,
 
 

Re:Different ways to create a form

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 am 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 is 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
 

Re:Different ways to create a form

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)
jvcl.sourceforge.net
 

Re:Different ways to create a form

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" <XXXX@XXXXX.COM>writes
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)
jvcl.sourceforge.net
 

Re:Different ways to create a form

"Reid Roman" <XXXX@XXXXX.COM>writes
Thank You all!
Disclaimer:
------------------------------
Filtering now in effect. If I
don't reply, it is because I
don't see it.