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

时间:2017-07-26 19:02:23

标签: delphi delphi-10-seattle

这是我的代码:

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分配给OnClose的{​​{1}}事件时,我收到以下错误消息:

  

[Dcc32错误] Unit1.pas(40):E2010不兼容的类型:'TCloseEvent'和'Procedure'

当我将其更改为:

Form2

一切正常,但就像Form2.OnClose := FormClose; ,而不是Self.FormClose程序。

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

1 个答案:

答案 0 :(得分:3)

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.