在Delphi XE中将类作为过程的参数传递

时间:2011-12-13 19:38:58

标签: forms delphi class delphi-xe factory

我需要做的是这样的事情:

procedure A(type_of_form);
var form: TForm;
begin
  form := type_of_form.Create(application);
  form.showmodal;
  freeandnil(form);
end;

我为每个动态创建的表单执行了此操作:

form1 := TForm1.Create(application);
form1.showmodal;
freeandnil(form1);

我将在程序A中做什么更复杂,但问题在于如何使表单的创建有点笼统。也许@运营商的事情......我真的不知道。

感谢您的任何建议!

2 个答案:

答案 0 :(得分:7)

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;

答案 1 :(得分:3)

你要求的基本上是TApplication.CreateForm()所做的,例如:

Application.CreateForm(TForm1, form1); 
form1.ShowModal; 
FreeAndNil(form1); 

您无需像这样调用TAppliction.CreateForm()即可模仿:

procedure A(AClassType: TFormClass); 
var
  form: TForm; 
begin 
  form := AClassType.Create(Application); 
  try
    form.ShowModal; 
  finally
    FreeAndNil(form);
  end; 
end; 

...

begin
  A(TForm1); 
end;