代码如下:
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    curForm :Tform;
    procedure ShowFormToClient(formClass:Tformclass;frmClient:Tform);
    { Public declarations }
  end;var
  Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowFormToClient(TForm2,Form2);
end;procedure TForm1.ShowFormToClient(formClass:Tformclass;frmClient:Tform);
begin
  if (curForm <> nil) then
  begin
    if  curForm.ClassType <> formClass then
      curForm.Free
    else
      exit;
  end;
  frmClient := formClass.Create(Application);
  frmClient.Parent := Panel1;
  frmClient.Align := alClient;
  frmClient.BorderStyle := bsNone;
  frmClient.Show;
  curForm := frmClient;
  //showmessage(curForm.ClassName);
end;/////////////////////////////////////////
type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
begin
  close;
end;procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := Cafree;
end;第一次创建form2后关闭,再创建FORM2时,curform 不为 nil ,请问该如何处理?谢谢!

解决方案 »

  1.   

    delphi 不会自动的设引用地址为 nil,
      

  2.   

    谢谢楼上的回答,
    但我不想每次调用ShowFormToClient时都把它释放,而是判断如果curform与frmClient为同一类就不执行创建,这个是我的目的。
      

  3.   

    问题解决,在close之前,把 cruform := nil ,但不知道这样安全不安全?因为可能需要多次创建和释放frmClient。
      

  4.   

    curForm.Free
    改为
    freeandnil(curForm)
      

  5.   

    在close之前,把 cruform := nil 我也是这样做的
      

  6.   

    原来没理解你的要求在close之前,把 cruform := nil,是安全的不过你看清楚, OnClose 事件中还有个 Action, 这个是说 Form 关闭后的行为, 设为 caFree;在外就没必要作 Free 了, 关闭时自动会释放