Kylix
Tips

NEW TIPS
Database (90)
Files (138)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Search Tip
Top15
Add new Tip

Forum

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

Tip Rating (41):  
     



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