what i need to do is something like this:
procedure A(type_of_form);
var form: TForm;
begin
form := type_of_form.Create(application);
form.showmodal;
freeandnil(form);
end;
I did this for every dynamically created form:
form1 := TForm1.Create(application);
form1.showmodal;
freeandnil(form1);
What i will do inside procedure A is more complex, but the problem resides in how to make the creation of the form somewhat general. Perhaps something with @ operator... i really do not know.
Thanks for any suggestion!
procedure Test(AMyFormClass: TFormClass);
var
form: TForm;
begin
form := AMyFormClass.Create(Application); // you can use nil if you Free it in here
try
form.ShowModal;
finally
form.Release; // generally better than Free for a Form
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Test(TForm2);
end;
What you are asking for is basically what TApplication.CreateForm()
does, eg:
Application.CreateForm(TForm1, form1);
form1.ShowModal;
FreeAndNil(form1);
You can mimic that without calling TAppliction.CreateForm()
like this:
procedure A(AClassType: TFormClass);
var
form: TForm;
begin
form := AClassType.Create(Application);
try
form.ShowModal;
finally
FreeAndNil(form);
end;
end;
...
begin
A(TForm1);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With