 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Reid Roman Guest
|
Posted: Thu Jul 24, 2003 7:37 pm Post subject: Different ways to create a form |
|
|
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
|
Posted: Thu Jul 24, 2003 7:54 pm Post subject: 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'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
|
Posted: Thu Jul 24, 2003 8:11 pm Post subject: 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" <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
|
Posted: Thu Jul 24, 2003 8:51 pm Post subject: Re: Different ways to create a form |
|
|
"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 |
|
 |
|
|
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
|
|