Kylix
Tips

NEW TIPS
Database (98)
Files (1187)
Forms (125)
Graphic (126)
IDE (30)
Indy (13)
Internet / LAN (146)
IntraWeb (12)
Math (84)
Misc (137)
Multimedia (56)
Objects/
ActiveX (64)

OpenTools API (14)
Printing (47)
Strings (94)
System (275)
VCL (246)

Search Tip
Top15
Add new Tip

Forum

...change the MainForm at runtime?
Author: Ahmed Ammar
[ Print tip ]    

Tip Rating (38):  
     



procedure SetAsMainForm(aForm:TForm);
var
  
P:Pointer;
begin
  
P := @Application.Mainform;
  Pointer(P^) := aForm;
end;

{************************************}

// Example of Usage:

{
Question:

  If my application is main.exe and the main form is form1, form1 displays
  when it runs. I would like to display other forms based on the parameter
  passed.
  main.exe param1 will display form2 as the first form
  main.exe param2 with display form3 as the first form

Answer:
}

program Project1;

uses
  
Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3};

{$R *.res}

procedure SetAsMainForm(aForm:TForm);
var
  
P:Pointer;
begin
  
P := @Application.Mainform;
  Pointer(P^) := aForm;
end;

begin
  
Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.CreateForm(TForm3, Form3);

  if Paramstr(1) = 'Param1' then
    
SetAsMainForm(Form2);

  if Paramstr(1) = 'Param2' then
    
SetAsMainForm(Form3);

  Application.Run;
end.

Rate this tip:

poor
very good


Copyright  Torry's Delphi Pages Torry's Delphi Pages Maintained by Simon Grossenbacher Notes? Comments? Feel free to send... Copyright 1996-2001