C
ckylixj
Unregistered / Unconfirmed
GUEST, unregistred user!
各位老师,我动态创建了多个Timer控件,并且为这些Timer控件的ontimer事件编写了一段代码(参数不同),我现在遇到的问题是没有办法将参数传递进去。
如果不传递参数,下面的代码可以编译成功
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure test1( sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
procedure TForm1.test1( sender: TObject);
begin
memo1.Lines.Add(FormatdateTime('yyyy-mm-dd hh:nn:ss',Now()));
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
time : TTimer;
begin
time := TTimer.Create(form1);
time.Interval :=2000;
time.OnTimer := test1;
time.Enabled := true;
end;
end.
如果传递了参数(ss:string),编译则报错。 [Erro]Unit1.pas(42): Incompatible type: 'TObject' and 'String
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
// procedure test1( sender: TObject);
procedure test1( sender: TObject;var ss :string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
procedure TForm1.test1( sender: TObject;var ss :string);
begin
memo1.Lines.Add(ss + FormatdateTime('yyyy-mm-dd hh:nn:ss',Now()));
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
time : TTimer;
begin
time := TTimer.Create(form1);
time.Interval :=2000;
time.OnTimer := test1('aaa');
time.Enabled := true;
end;
end.
如果不传递参数,下面的代码可以编译成功
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure test1( sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
procedure TForm1.test1( sender: TObject);
begin
memo1.Lines.Add(FormatdateTime('yyyy-mm-dd hh:nn:ss',Now()));
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
time : TTimer;
begin
time := TTimer.Create(form1);
time.Interval :=2000;
time.OnTimer := test1;
time.Enabled := true;
end;
end.
如果传递了参数(ss:string),编译则报错。 [Erro]Unit1.pas(42): Incompatible type: 'TObject' and 'String
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
// procedure test1( sender: TObject);
procedure test1( sender: TObject;var ss :string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
procedure TForm1.test1( sender: TObject;var ss :string);
begin
memo1.Lines.Add(ss + FormatdateTime('yyyy-mm-dd hh:nn:ss',Now()));
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
time : TTimer;
begin
time := TTimer.Create(form1);
time.Interval :=2000;
time.OnTimer := test1('aaa');
time.Enabled := true;
end;
end.