不兼容的类型:'TCloseEvent' 和 'Procedure'

Incompatible types: 'TCloseEvent' and 'Procedure'

这是我的代码:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

  TForm2 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
    // I try to put it here but the same problem
    //procedure FormClose(Sender: TObject; var Action: TCloseAction);
  end;
var
  Form1: TForm1;
  Form2: TForm2;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Form2 := TForm2.CreateNew(Application);
Form2.Parent := Self;
Form2.OnClose := TForm2.FormClose;
Form2.Show;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ShowMessage('Form1Close');
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   ShowMessage('Form2Close');
end;
end.

当我尝试将 FormClose 分配给 Form2OnClose 事件时,我收到以下错误消息:

[Dcc32 Error] Unit1.pas (40): E2010 Incompatible types: 'TCloseEvent' and 'Procedure'

当我把它改成:

Form2.OnClose := FormClose;

它工作正常,但这就像 Self.FormClose,而不是 TForm2.FormClose 程序。

如何将 TForm2.FormClose 分配给 Form2.Onclose

TForm2.FormClose改为Form2.FormClose:

Form2.OnClose := Form2.FormClose;

但是,由于您希望 OnClose 事件与属于您刚刚创建的同一对象的处理程序相关联,因此最好将 TForm2 移动到一个单独的单元它自己的设计时 DFM,然后您可以在设计时分配 TForm2.OnClose 事件,并在运行时调用 TForm2.Create() 时让 DFM 为您连接它:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(Application);
  Form2.Parent := Self;
  Form2.Show;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ShowMessage('Form1Close');
end;

end.

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   ShowMessage('Form2Close');
end;

end.