Just add Ken White's answer.
If you look at the source for CreateForm:
procedure TApplication.CreateForm(InstanceClass: TComponentClass; var Reference); var Instance: TComponent; begin Instance := TComponent(InstanceClass.NewInstance); TComponent(Reference) := Instance; try Instance.Create(Self); except TComponent(Reference) := nil; raise; end; if (FMainForm = nil) and (Instance is TForm) then begin TForm(Instance).HandleNeeded; FMainForm := TForm(Instance); end; end;
You see that a function (despite its name) can be used to create other components. But the main form can only be the first component, which is TForm and which is created successfully.
And then rant about global variables.
Yes, global variables are often erroneous, but you can make an exception for the application object and the main form object. Although you can omit the global value for the main form, you need to edit the dpr file yourself:
Edit:
begin Application.Initialize; Application.CreateForm(TMyMainForm, MyMainFormGlobal); Application.Run end.
To:
procedure CreateMain; var mainform : TMyMainForm; begin Application.CreateForm(TMyMainForm, mainform); end; begin Application.Initialize; CreateMain; Application.Run end.
And you have lost all global forms.
Toon krijthe
source share