本文介绍方法一、
新建一个工程文件,建form1,form2,其中form1加入一个按钮控件控件,
写事件如下
uses unit2;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;//form2在工程文件中已创建 Application.CreateForm(TForm2, Form2);
WriteComponentResFile('D:\unit2.dfm',form2);
end;
Form2中放置一个pagecontrol1,
写代码如下
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;
type
TForm2 = class(TForm)
PageControl1: TPageControl;
procedure FormCreate(Sender: TObject);
procedure fButtonClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
fButton:array of array of TButton;
fnewpage:array of TTabSheet;
fstr1,fstr2:array of array of String;
public
procedure ClearControls(AParent: TTabSheet);
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.fButtonClick(Sender: TObject);
var
s,s1,s2:string;
i,j,x:Integer;
begin
//
s:= (Sender as TButton).Name;
x:=pos('j',s);
s1:=copy(s,4,x-4);
s2:=copy(s,x+1,length(s)-x);
i:=strtoint(s1);
j:=strtoint(s2);
showmessage(s);
end;
procedure TForm2.FormCreate(Sender: TObject);
const
col2=5;
lrAside=10;
width1=160;
height1=40;
btnMagins=20;
var
sc,sc1:string;
rc,rd:Integer;
i,j:Integer;
st:String;
begin
begin
rc:=10;
SetLength(fnewpage,rc);
SetLength(fButton,rc);
SetLength(fstr1,rc);
SetLength(fstr2,rc);
for I := 0 to rc-1 do begin
fnewpage[I]:=TTabSheet.Create(pagecontrol1);
sc1:='测试模块'+inttostr(I);
fnewpage[I].Caption:=sc1;
fnewpage[i].Name:='fsc'+inttostr(i);
fnewpage[I].PageControl:= PageControl1;
PageControl1.ActivePage:=fnewpage[I];
rd:=20;
SetLength(fButton[I],rd);
SetLength(fstr1[I],rd);
SetLength(fstr2[I],rd);
for J := low(fButton[I]) to high(fButton[I]) do
begin
fButton[i,j]:=TButton.Create(self);
fButton[i,j].Parent:=fnewpage[i];
fButton[i,j].Caption:='测试按钮('+inttostr(I)+':'+inttostr(J)+')';
fButton[i,j].Name:='fcx'+inttostr(i)+'j'+inttostr(j);
fButton[i,j].top:= ((J) div col2) * (height1+ lrAside) + lrAside ;
fButton[i,j].Left:=((J) mod col2) * (width1 + btnMagins) + lrAside;
fButton[i,j].Width:=width1;
fButton[i,j].Height:=height1;
st:='test'+ inttostr(I)+'j'+inttostr(J) ;
fButton[i,j].hint:=st;
fButton[i,j].OnClick:=fButtonClick;
end;
end;
end;
end;
procedure TForm2.FormDestroy(Sender: TObject);
var
I:Integer;
begin
for I :=High(fnewpage) downto Low(fnewpage) do
begin
ClearControls(fnewpage[i]);
fnewpage[i].Free;
end;
end;
procedure TForm2.ClearControls(AParent: TTabSheet);
var
i: Integer;
begin
for i := AParent.ControlCount - 1 downto 0 do begin
if not AParent.Controls[i].InheritsFrom(TWinControl) then Continue;
try
if TWinControl(AParent.Controls[i]).ControlCount > 0 then begin
ClearControls(TTabSheet(AParent.Controls[i]));
end;
AParent.Controls[i].Free;
except
end;
end;
end;
end.
运行form1,按下form1的按钮控件,会在d:\根目录地下生成unit2.dfm,只有.dfm文件没有关联的pas怎么办。拷贝上面的.pas文件,代码删除掉动态生成控件的部分,如下所示:
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;
type
TForm2 = class(TForm)
PageControl1: TPageControl;
procedure FormCreate(Sender: TObject);
procedure fButtonClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
public
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.fButtonClick(Sender: TObject);
var
s,s1,s2:string;
i,j,x:Integer;
begin
//
s:= (Sender as TButton).Name;
x:=pos('j',s);
s1:=copy(s,4,x-4);
s2:=copy(s,x+1,length(s)-x);
i:=strtoint(s1);
j:=strtoint(s2);
showmessage(s);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
//
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
//
end;
end.
此时的form2是如下表单模样
在新建的工程文件中打开这个unit2.pas和unit2.dfm,就可以正常设计和修改了