delphi 中怎样将事件设置为某个自己定义的函数?

来源:百度知道 编辑:UC知道 时间:2024/05/21 14:22:22
比如说我动态创建了一个timer,能不能将其ontimer事件指向我在之前定义的get函数?
是说动态的,不是在object inspector中的
问题是,上面的get函数怎么定义?
比如:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,extctrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
public
end;

var
Form1: TForm1;
timer:ttimer;
implementation
procedure mytimer;
begin
...
end;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
timer:=ttimer.Create(form1);
timer.OnTimer:=mytimer;
end;

end.

过不了编译,怎么改

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,extctrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure mytimer(Sender: TObject);//注意,把mytimer函数放到TForm1内部来定义
private

public
end;

var
Form1: TForm1;
timer:ttimer;
implementation

procedure TForm1.mytimer(Sender: TObject);
begin
...
end;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
timer:=ttimer.Create(form1);
timer.OnTimer:=mytimer;
end;

end.

应该可以

能的

timer.ontimer := MyOntimer;