Главная страница | назад





Article #26407: Dynamically changing the MainForm of an Application when it starts.

Developing Applications
By Corbin Dunn
Delphi Developer Support

Question: I want to dynamically change the MainForm for my Application based on a command line switch or registry setting. How can I do this?

Answer: The MainForm for your Application will be the first form created with a call to Application.CreateForm. Whenever this form is closed, your whole application will close. You may want to change which form this is based on a command line switch or registry setting. Here is a code sample from the Application source on how to do this:

program Project1;
uses
Forms,
Classes, // For TComponentClass
SysUtils, // For FindCmdLineSwitch
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.RES}
var
InstanceClass: TComponentClass;
FormReference: TForm;
begin
Application.Initialize;
if FindCmdLineSwitch('second', ['-', '/'], True) then
begin
InstanceClass := TForm2;
FormReference := Form2;
end
else
begin
InstanceClass := TForm1;
FormReference := Form1;
end;
Application.CreateForm(InstanceClass, FormReference);
Application.Run;
end.
Download the complete sample project from Borland CodeCentral

Last Modified: 12-DEC-00