Application.CreateForm has two purposes:
The first time it gets called, it sets the value of the
Application.MainFormproperty to refer to the newly created form. When the form represented by that property gets closed, the application will terminate.Through some tricks that aren’t relevant to this subject, the variable passed as the second parameter of that method gets to refer to the new form instance before the class’s constructor gets called, which means that you can use the variable within the form’s own
OnCreateevent handler. (OnCreategets called during the class’s constructor.)
Regarding the first point, it’s important to note that
MainForm gets set after the form is
finished being created, including after the form’s
OnCreate event handler runs. So, if your
OnCreate event handler has code to create an
additional form, and you use Application.CreateForm
to do it, then that second form will wind up being the
application’s main form. It’s not simply the first call to
CreateForm that defines the main form; it’s the first
call to finish.
Regarding the second point, it’s just poor style to use global
variables, especially ones you don’t even need. A common mistake is
to use the Form1 variable in the
TForm1.OnCreate event handler, for
instance. But within that event handler, you do not need a reference to
Form1. You already have the implicit
Self variable. If you happen to have multiple
instances of a form class, a single global variable will do you no good
since it cannot refer to both instances at once. The
Self variable will always refer to the correct
instance, though.
There should be exactly one call to
Application.CreateForm in a program, solely to allow
the MainForm property to get set. Any other use
of CreateForm is unnecessary, and it may even lead to
problems in your code, problems that can be difficult to diagnose. Any
other time you need to create a form, call its constructor directly.