尝试将功能分配给单击事件时出现不兼容的错误

时间:2014-02-18 04:39:43

标签: delphi events

我的表单上有一个按钮,单击按钮,表单标题将会更改。下面是我的工作代码。

type
  TForm1 = class(TForm)
    Button1: TButton;

    procedure FormShow(Sender: TObject);
    procedure setcaption(Sender: TObject);
  private
      { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
 { TForm1 }

procedure TForm1.setcaption (Sender: TObject);
begin
    ((sender as tbutton).Parent as tform).Caption := 'Success...';
end;


procedure TForm1.FormShow(Sender: TObject);
begin
  Button1.OnClick := setcaption(sender);
end;

收到错误

[Error] Unit1.pas(38): Incompatible types: 'TNotifyEvent' and 'procedure, untyped pointer or untyped parameter'

请纠正我的错误,我是这个概念的新手......

1 个答案:

答案 0 :(得分:1)

您必须使用以下内容:

Button1.OnClick := setcaption;

请检查作业声明中左侧和右侧的类型。左侧是TNotifyEvent类型。这意味着右侧的表达式必须属于同一类型,例如:是function,返回TNotifyEvent - 类型的值。显然,您的procedure电话不会这样做。

推荐阅读:http://docwiki.embarcadero.com/RADStudio/XE5/en/Procedural_Types#Procedural_Types_in_Statements_and_Expressions