本人在编写动态创建form的函数时碰到了困难,现在已经得知了要创建form的名称名,如果编写一个通用的函数,比如:MyCreateForm(××:string;。。。)的函数,来创建窗体呀,请高手指点。
PS:由于要创建的窗体很多,大概有50来个吧,所以只能编写函数来解决了
function MyCreateForm(MyForm:TForm):boolean;
begin
try
if not assigned(MyForm) then
MyForm:=TMyForm.Create(Application);
.
.
.
MyForm.Show;
result:=true;
except
result:=false;
end;
end;
如果功能和形式相似的话这是一个不错的办法;
手工:file\new\你工程的名字\你已经建立的窗体 只有Inherited 了:)
入口参数,和出口参数?
Code: function MyCreateForm():string;
begin
formName:=TForm.Create(self);
try
formname.show;
...
finally
formname.free;
end;
end;
新增Form1,Form2
Form1中的代碼:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, CheckLst;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
end;
function MyCreateForm(AName: String ;AForm: TForm): TForm;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
MyCreateForm(A,Form2).ShowModal;
end;
function MyCreateForm(AName: String ;AForm: TForm): TForm;
begin
AForm := Tform.Create(nil);
AForm.Name := AName;
Result := AForm;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MyCreateForm(B,Form2).ShowModal;
end;
end.