以下是为你作的一个简单的例子,看看你能不能用来参考一下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Unit2;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
function GetPanel1Form2: TForm2;
function CreateForm2(AParent: TPanel): TForm2;
procedure InfoForm2(AForm2: TForm2);
property ClientForm2: TForm2 read GetPanel1Form2;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{
用来返回一个 TForm2 的实例
}
function TForm1.GetPanel1Form2: TForm2;
var
i: Integer;
begin
Result := nil;
//在 Panel1 中查找 TForm2 的实例
for i := 0 to Panel1.ControlCount -1 do
begin
if Panel1.Controls is TForm2 then
begin
Result := TForm2(Panel1.Controls);
Break;
end;
end;
//如果 Panel1 中没有 TForm2 的实例,那么就创建一个
if Result = nil then
begin
Result := CreateForm2(Panel1);
InfoForm2(Result);
end;
end;
{
创建 TForm2 的一个实例,在创建前先释放原来的实例
创建完成后设置 Parent := AParent 即 Panel1
}
function TForm1.CreateForm2(AParent: TPanel): TForm2;
var
i: Integer;
begin
//释放原来的 TForm2 的实例
for i := 0 to AParent.ControlCount - 1 do
begin
if AParent.Controls is TForm2 then
TForm2(AParent.Controls).Free;
end;
Result := TForm2.Create(self);
Result.Parent := AParent;
end;
{
设置新创建的 TForm2 的一些属性
}
procedure TForm1.InfoForm2(AForm2: TForm2);
begin
with AForm2 do
begin
//caption:=xxx(变量)
//label1.font.color:=变量
//......
//这里就不需要 parent:=panel1; 了
Show;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
InfoForm2(CreateForm2(Panel1)); //创建并设置 TFrom2 的实例
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
with ClientForm2 do
begin
CheckBox1.Checked := False;
//......
end;
end;
end.