gpt4 book ai didi

delphi - 如何动态创建表单

转载 作者:行者123 更新时间:2023-12-05 02:26:22 28 4
gpt4 key购买 nike

我有一个 Delphi 10.4 应用程序,其中包含 20 多个动态创建的表单。有一个函数可以创建如下形式:

Procedure SetForm(nForm : ShortInt);
Begin
Case nForm of
1: begin
If not Assigned(Form1) then
Application.CreateForm(TForm1, Form1);
Form1.Show;
End;
2: begin
If not Assigned(Form2) then
Application.CreateForm(TForm2, Form2);
Form2.Show;
End;

End;

问题是:我可以创建一个通用函数来创建如下形式:

Procedure SetForm(nForm: ShortInt);
Begin
xForm : TForm;
xForm := arrayForm[nForm]; // Array containing all forms;
if not Assigned(xForm) then
Application.CreateForm((some cast as TComponentClass), xForm);
xForm.Show;
end;

使事情复杂化的是一些表单有一个函数需要在显示之前执行,比如:

xForm.SetUser(nUser);

我试过这个只是为了创建和激活表单

...
type
TFormInfo = record
ClassType: TFormClass;
Form: TForm;
end;

procedure CreateForm(nForm: ShortInt);

var
arrayForm: array[1..2] of TFormInfo = (
(ClassType: TFormParam; Form : nil),
(ClassType: TFormCliGrid; Form: nil)
);

implementation

procedure CreateForm(nForm:ShortInt);
var xForm:TForm;
begin
xForm := arrayForm[nForm].Form;
if not Assigned(xForm) then
begin
xForm := arrayForm[nForm].ClassType.Create(Application);
arrayForm[nForm].Form := xForm;
end;
xForm.Show;
end;

当我尝试在 FormActivate 上更改第二个表单 (FormCliGrid) 中的组合框时

...
comboStatus.Items.BeginUpdate;

我得到了“访问冲突”

最佳答案

是的,您所要求的是非常可行的,将元类与 TForm 的虚拟构造函数一起使用。

例如,您可以创建一个基类来访问所有表单的通用功能,例如:

type
TMyBaseForm = class(TForm)
public
procedure SetUser(nUser: UserTypeHere); virtual; abstract;
end;

TMyBaseFormClass = class of TMyBaseForm;
type
TForm1 = class(TMyBaseForm)
public
procedure SetUser(nUser: UserTypeHere); override;
end;

TForm2 = class(TMyBaseForm)
public
procedure SetUser(nUser: UserTypeHere); override;
end;

...
type
TFormInfo = record
ClassType: TMyBaseFormClass;
Form: TMyBaseForm;
end;

var
arrayForm: array[0..1] of TFormInfo = (
(ClassType: TForm1; Form: nil),
(ClassType: TForm2; Form: nil)
);

Procedure SetForm(nForm: ShortInt);
Var
xForm : TMyBaseForm;
Begin
xForm := arrayForm[nForm].Form;
if not Assigned(xForm) then
begin
xForm := arrayForm[nForm].ClassType.Create(Application);
arrayForm[nForm].Form := xForm;
end;
xForm.SetUser(nUser);
xForm.Show;
end;

或者,如果不能使用基类,则可以使用接口(interface),例如:

type
ISetUser = interface
['{6cc8854b-f945-4a0d-ab13-624a13eaade1}']
procedure SetUser(nUser: UserTypeHere);
end;
type
TForm1 = class(TForm, ISetUser)
public
procedure SetUser(nUser: UserTypeHere);
end;

TForm2 = class(TForm, ISetUser)
public
procedure SetUser(nUser: UserTypeHere);
end;

...
type
TFormInfo = record
ClassType: TFormClass;
Form: TForm;
end;

var
arrayForm: array[0..1] of TFormInfo = (
(ClassType: TForm1; Form: nil),
(ClassType: TForm2; Form: nil)
);

Procedure SetForm(nForm: ShortInt);
Var
xForm : TForm;
Intf: ISetUser;
Begin
xForm := arrayForm[nForm].Form;
if not Assigned(xForm) then
begin
xForm := arrayForm[nForm].ClassType.Create(Application);
arrayForm[nForm].Form := xForm;
end;
if Supports(xForm, ISetUser, Intf) then
Intf.SetUser(nUser);
xForm.Show;
end;

关于delphi - 如何动态创建表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73807219/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com