【问题标题】:How to use MessageDlg in OnClose Event of Delphi Form?Delphi Form的OnClose事件中如何使用MessageDlg?
【发布时间】:2012-11-27 09:45:56
【问题描述】:

当我通过单击“交叉按钮”或 Alt + F4 关闭表单时,我希望用户询问他是否要关闭应用程序。如果是,我将终止申请,否则无事可做。我在表单的 onclose 事件上使用以下代码

procedure MyForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
  buttonSelected : integer;
begin
  buttonSelected := MessageDlg('Do you really want to close the application?',mtCustom, [mbYes,mbNo], 0);
  if buttonSelected = mrYES then
  begin
    Application.Terminate;
  end
  else
  begin
    //What should I write here to resume the application
  end;

end;

无论我单击“是”还是“否”,我的应用程序都将终止。我应该怎么做才能在没有点击确认框时,我的应用程序不应该终止。我应该如何改进我的上述功能?我是否使用正确的表单事件来实现此功能?请帮忙..

【问题讨论】:

  • 您必须使用Action 参数,但对于OnCloseQuery 事件来说这是更好的任务。
  • 如果这是您的主要表单,那么您无需致电Application.Terminate。关闭主窗体后,应用程序将关闭。
  • 我讨厌应用程序询问我是否真的很确定我希望它消失。
  • @sertacAkyuz - 但这是要求
  • @DavidHeffernan - 好的,删除 application.terminate

标签: delphi dialog delphi-xe2


【解决方案1】:
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
  buttonSelected: integer;
begin
  buttonSelected := MessageDlg('Do you really want to close the application?', mtCustom, [mbYes, mbNo], 0);
  if buttonSelected = mrYES then
  begin
    CanClose:=true;
  end
  else
  begin
    CanClose:=false;
  end;
end;

或者按照@TLama 的建议,简化:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := MessageDlg('Do you really want to close the application?', mtCustom, [mbYes, mbNo], 0) = mrYES;
end;

【讨论】:

  • 也可以简化为一行CanClose := MessageDlg('Do you really want to close the application?', mtCustom, [mbYes, mbNo], 0) = mrYES;
【解决方案2】:

如果您键入,窗口将保持打开状态

Action := caNone;

在你的 else 部分

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2012-10-28
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多