我在for循环里动态初始化好几个timer,这样当一个timer到时,我怎么知道是哪个 timer呢?
   所以我想,在初始化timer时,就把 timer绑定的 机器号传到 它的ontimer里,这个 item的ontimer后,可以很简单 知道是哪个 机器?
---------------------------------
我新建多个计时器,现在想用 自定义函数 处理到时,却不知道怎么做
   下面的代码 编译出错
unit MainForm;
interface


type
  TForm1 = class(TForm)
  ctl1: TCnTimerList;

  procedure dao(Sender:TObject;PCNum:integer);//自定义的 ontimer
implementation

//需要执行的操作
procedure TForm1.dao(Sender:TObject;PCNum:integer);//自定义的 ontimer
begin
  ShowMessage(IntToStr(PCNum));
end;procedure createTimerList();
var i:Integer;
begin
  for I := 1 to config1.PcNumber do
    begin
      Form1.ctl1.Items.Add;
      Form1.ctl1.Items.Enabled := False;
      Form1.ctl1.Items.OnTimer := Form1.dao(?,i) ;//接管,i是要传的 自定义参数。编译出错
    end;
end;

解决方案 »

  1.   

    1。一个小小建议,可以利用Timer.Tag来标示你Timer,比如Timer[0].Tag:=0,Timer[1].Tag=1 ....
      这样在Timer.OnTime事件中 通过 TTimer(Sender).Tag就可以判断出是哪个Timer了
      

  2.   

    还有所有的Timer的OnTimer事件都应指向同一OnTimer事件响应处理过程
      

  3.   

    procedure TForm1.MyTimer(Sender: TObject);
    begin
      ShowMessage(IntToStr(TTimer(Sender).Tag));
    end;procedure TForm1.CreateTimerList(); 
    var 
      i: Integer; 
    begin 
      for i := 1 to config1.PcNumber do 
      begin
        with TTimer.Create(Self) do
        begin
          Enabled := False;
          Tag := i;
          Interval := 3000;
          OnTimer := MyTimer;
          Enabled := True;
        end;
      end;
    end;
      

  4.   

    最好用重定义 ontimer,传递参数的方法做。
    因为事实上我把timer做成了一个list
      

  5.   

    楼主用的是TCnTimerList,它的每个定时器没有tag,这样实现是可以的:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, CnClasses, CnTimer;type
      TForm1 = class(TForm)
        Button1: TButton;
        ctl1: TCnTimerList;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        PCNum:array [0..5] of integer;
        procedure dao(Sender:TObject);//自定义的 ontimer
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    {$R *.dfm}
    procedure TForm1.dao(Sender:TObject);//自定义的 ontimer
    begin
     Edit1.Text:= IntToStr(PCNum[(Sender as TCnTimerItem).Index]);end;
    procedure TForm1.Button1Click(Sender: TObject);
    var i:Integer;
    begin
      for I := 0 to 5 do
        begin
          PCNum[i]:=i;      //i是要传的 自定义参数。
           Form1.ctl1.Items.Add;
          Form1.ctl1.Items[i].Interval:=5000;
          Form1.ctl1.Items[i].Enabled := False;
           Form1.ctl1.Items[i].OnTimer := Form1.dao ;//接管
        end;
         Form1.ctl1.Items[3].Enabled := true;     //测试,开第03号
    end;end.