我有这个程序:
procedure TForm1.Button1Click(Sender: TObject);
var
a:TForm2;
begin
a := TForm2.Create(Self);
a.Parent := ScrollBox1;
a.Align := alClient;
a.Show;
a.SetFocus;
end;
Run Code Online (Sandbox Code Playgroud)
我将上面的代码更改为此,但我收到错误,为什么?我必须将此代码更改为?
procedure TForm1.MakeAform(aForm:Tform;Cmp:TComponent;Parent1:TWinControl;Align1:TAlign);
var
a:aForm; // Error Here
begin
a := aForm.Create(Cmp);
a.Parent := Parent1;
a.Align := Align1;
a.Show;
a.SetFocus;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MakeAform(Tform2,Self,Panel1,alClient);
end;
Run Code Online (Sandbox Code Playgroud)
您的原始代码将类(TForm2)传递给接收实例(aForm)的过程.事实上,这个实例甚至没有初始化,但实际上这不是你的问题.
您需要做的是在MakeAform中接收类而不是实例.
你的代码应该是:
//note, in Forms.pas the type TFormClass is defined as:
// TFormClass = class of TForm;
//
//A variable of TFormClass holds a class (rather than an instance)
//and that class must be derived from TForm.
procedure TForm1.MakeAform(
FormClass: TFormClass;
Owner: TComponent;
Parent: TWinControl;
Align: TAlign
);
var
a: TForm;
begin
a := FormClass.Create(Owner);
a.Parent := Parent;
a.Align := Align;
a.Show;
a.SetFocus;
end;
Run Code Online (Sandbox Code Playgroud)
还有几点:
| 归档时间: |
|
| 查看次数: |
1588 次 |
| 最近记录: |