Recent

Author Topic: Create a form object from its class type  (Read 1540 times)

Bruce44

  • New Member
  • *
  • Posts: 15
Create a form object from its class type
« on: November 16, 2021, 09:54:08 pm »
Hello
I have two different forms type TForm 1 and TForm2.
I would like to create one form object of each form type with a generic procedure
Exemple :
LoadForm (TForm1);
LoadForm (TForm2);

And the procedure :
procedure LoadForm (a_form_class : TClass);
var
   o_form : TForm;
begin
   o_form := TForm(a_form_class).Create (Form_Main);
end;

But this crashes ! It may be not the correct syntax ! An idea ? Another suggestion ?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Create a form object from its class type
« Reply #1 on: November 16, 2021, 10:16:08 pm »
No need to reinvent the wheel.
Such a routine already exists in the Forms unit,
Code: Pascal  [Select][+][-]
  1. procedure TApplication.CreateForm(InstanceClass: TComponentClass; out Reference);

You call it thus:
Code: Pascal  [Select][+][-]
  1. Application.CreateForm(TForm1, Form1);
  2. Application.CreateForm(TForm2, Form2); etc.
« Last Edit: November 16, 2021, 10:19:10 pm by howardpc »

Bruce44

  • New Member
  • *
  • Posts: 15
Re: Create a form object from its class type
« Reply #2 on: November 16, 2021, 10:22:55 pm »
Of course ! So obvious ;)
Thanks

 

TinyPortal © 2005-2018