Free Pascal / Lazarus:为什么 "FormCloseQuery" 事件在我的例子中没有被调用?

Free Pascal / Lazarus : Why is the "FormCloseQuery" Event not called in my example?

我编写了一个小测试程序来试用 FormCreate 和 FormCloseQuery 过程。 FormCreate 工作正常,但 FormCloseQuery 只是不想执行。我忽略了什么吗?在表单上按 "X"-window 或使用关闭方法,两者都不起作用!

procedure FormCreate(Sender: TObject); 
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
(...)


procedure TForm1.FormCreate(Sender: TObject); 
  beginn
  //gets executed without problems
  end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  begin

  case MessageDlg('Question', mtConfirmation, [mbyes, mbno, mbcancel], 0) of

  mrYes:
  begin
    ShowMessage('yes');
    CanClose := true;
    end;

  mrNo:
  begin
    ShowMessage('no');
    CanClose := true;
    end;

  else
  begin
    ShowMessage('cancel');
    CanClose := true;
    end;

  end;
  end;

完整代码如下:

lpr 文件:

program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, Unit1
  { you can add units after this };

{$R *.res}

begin
  RequireDerivedFormResource:=True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

单元文件:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShowMessage('FormCreate Procedure wurde gestartet');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin

  case MessageDlg('Question', mtConfirmation, [mbyes, mbno, mbcancel], 0) of

  mrYes:
  begin
    ShowMessage('yes');
    CanClose := true;
    end;

  mrNo:
  begin
    ShowMessage('no');
    CanClose := true;
    end;

  else
  begin
    ShowMessage('cancel');
    CanClose := true;
    end;

  end;
end;
end.

解决方案is/was 将表单的表单的 OnCloseQuery 事件连接到此过程。以下是如何操作的简短说明:

  • 在对象检查器中调出并 select 表单(在我的例子中 "Form1: TForm1")(见下面的解释)。
  • 在对象检查器中转到选项卡 "events"
  • 找到 "OnCloseQuery"-Event 和 select "FormCloseQuery" 如果您已经像我的示例中那样声明并编写了过程。 (如果你没有 declared/implemented 它双击下拉框或点击它旁边的按钮,带有三个点的按钮,一个空的 "OnCloseQuery"-procedure 程序将自动添加。代码编辑器将直接跳转到新过程。)

要在对象检查器中调出/select 窗体,请打开对象检查器 (F11)。如果您正在查看代码编辑器,请先调出表单 (F12) 并单击它。这应该在对象检查器中显示它。确保表单(列表中的顶部元素)是 selected 并且它的组件(如按钮等)是 none。

这是另一种解释:http://www.delphigroups.info/2/b2/444056.html