Delphi避免重复打开窗体

时间:2023-03-09 06:43:15
Delphi避免重复打开窗体

取消自动创建窗体

Delphi避免重复打开窗体

Form1关键代码
implementation

uses
Unit2; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
begin
if not Assigned(Form2) then //assigned 是用来判断某一指针(pointer)或过程引用是否为nil(空),如果为空则返回假(false)。
begin
Form2 := TForm2.Create(Self);
Form2.Show;
end;
end;
Form2关键代码需要在Form2里释放资源
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Form2:=nil; //Form对象指向空地址
Action := caFree; //Form关闭后释放占用的内
end;