这可能不是我的应用程序(XE7)的问题,因为它只发生在我的一个客户工作站上,但我想确认我正在编码这个并要求任何有关解决它的指导
从应用程序主表单中可以启动一个非模态表单,然后当其中最后一个表单关闭时,将应用程序主表单放在Windows Z顺序的后面
这是主要应用的启动代码
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TDM, DM);
Application.CreateForm(TfmMain, fmMain);
Application.CreateForm(TfmReportDefn, fmReportDefn);
Application.Run;
以下是启动非模态表单的代码示例
if (fmCustomer = nil) then
fmCustomer := TfmCustomer.Create(nil);
else
fmCustomer.SetFocus;
这是非模态形式的onclose事件代码
procedure TfmCustomer.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action := cafree;
fmCustomer := nil;
end;
感谢您的回复
答案 0 :(得分:0)
以下是启动非模态表单的代码示例
if (fmCustomer = nil) then fmCustomer := TfmCustomer.Create(nil); else fmCustomer.SetFocus;
我会建议更像这样的东西:
if (fmCustomer = nil) then
fmCustomer := TfmCustomer.Create(Application);
fmCustomer.Show;
首先,分配所有者可确保在应用关闭且用户尚未关闭表单时仍自动释放表单。
其次,使用Show()
代替SetFocus()
,以确保表单实际可见并显示在前台。
这是非模态形式的onclose事件代码
procedure TfmCustomer.FormClose(Sender: TObject; var Action: TCloseAction); begin action := cafree; fmCustomer := nil; end;
使用caFree
很好,但我建议您将nil
转让转移到OnDestroy
活动中:
procedure TfmCustomer.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TfmCustomer.FormDestroy(Sender: TObject);
begin
fmCustomer := nil;
end;
仅当用户手动关闭窗口,操作系统正在关闭或代码调用OnClose
时,才会触发fmCustomer.Close()
事件。如果表单因其他原因而被释放,您仍然需要nil
指针,但不会调用OnClose
。