主Form建议多页面程序,一般都将子Form存放在一个List里,
procedure TForm1.FormCreate(Sender: TObject); begin FList:=TStringList.Create; form2:=tForm2.Create(Application); Form2.Parent:=Self.Panel1; Form2.Show; FList.AddObject(‘11‘,Form2); end;
程序退出,应该将子Form对将注销掉,例如下面的代码。
procedure Tform1.FreeForm; var I:integer; begin IF not Assigned(FList) then exit; for I:=0 to FList.Count-1 do begin IF not Assigned(FList.Objects[I]) then Continue; TForm(FList.Objects[I]).Free; FList.Objects[I]:=nil; end; FreeAndNil(FList); end;
但我们不要将他放在FormDestroy里面,
procedure TForm1.FormDestroy(Sender: TObject); begin FreeForm; end;
而应该在FormDestroy之前,例如FormClose里将他之注销
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin IF Self.chkFreeChildForm.Checked then FreeForm; end;
否则退出时会出现AV错误
如果你一定要在FormDestroy中执行 FreeForm,那么建方子窗口时不要用 form2:=tForm2.Create(Application);而是用 form2:=tForm2.Create(nil);或者 form2:=tForm2.Create(self);