大概是这样的Tmycomp=class(Tcomponent)
private
 timer:Ttimer;public
 oncalculate:TnotifyEvent;
 x:integer;
timer:ontimer(..)
begin
 x:=x+1;
..... 即每Timer执行一回就会产生oncalculate事件
end;建好类,我用的时候就这样用:
Acmp:TmyComp;
Acmp.onCalculate(..)
begin
 showMessage(intTostr(Acmp.x));
end;
总的说来就是:怎么使自己的组件或类能产生一个事件,就像Timer:onTimer这样的事件谢谢先

解决方案 »

  1.   

    自己写了个测试的,不行啊。。5555unit UntEventProp;
    uses QExtCtrls,classes,controls,QDialogs;
    type
     TObj=class(Tcomponent)
     private
      timer:Ttimer;
      Foncal:TNotifyEvent;
      procedure Timer1Timer(Sender: TObject);
     public
       x:integer;
      property onCal: TNotifyEvent read FonCal write FOnCal;
      constructor Create(AOwner: TComponent);override;
      destructor destroy;override;
     end;implementation{ TObj }constructor TObj.Create(AOwner: TComponent);
    begin
      inherited;
      timer:=Ttimer.Create(self);
      timer.Interval:=50;
      timer.Enabled:=true;
      timer.OnTimer:=Timer1Timer;
    end;destructor TObj.destroy;
    begin
    timer.Free;
    inherited;
    end;procedure TObj.Timer1Timer(Sender: TObject);
    begin
       x:=x+1;
       FonCal(self);            //关键这里怎么触发Foncal事件?
    end;
    {==========================================================================}
    在Form1中用的方式:procedure TForm1.FormCreate(Sender: TObject);
    begin
        obj:=Tobj.create(self);
        obj.oncal:=self.oncal;    /////  这里赋给TObj.oncal
    end;procedure TForm1.oncal(sender: Tobject);
    begin
     memo1.lines.add(intTostr(obj.x));   ///
    end;可惜Memo1 根本就没反应。。奇怪了,也就是
       FonCal(self);            //这里根本就没被触发过??麻烦大家看下,我不知道咋办了~~
      

  2.   


    1.加入一个专用的timer,
    2.对此timer设定时间,
    3.用此timer的ontimer事件就可作为你的onCal()事件,
      
      

  3.   

    我也有点乱了,原来的问题解决掉了,FonCal(self); 是可以的,问题出在Timer上现在的问题是:Obj.Timer不会自动运行?Tmycomp=class(Tcomponent)
    private
     timer:Ttimer;
    ...constructor TObj.Create(AOwner: TComponent);
    begin
      inherited;
      timer:=Ttimer.Create(self);
      timer.Interval:=50;
      timer.Enabled:=true;
      timer.OnTimer:=Timer1Timer;
    end;procedure TObj.Timer1Timer(Sender: TObject);
    begin
       x:=x+1;
       FonCal(self);           
    end;
    为什么Timer.ontimer赋值后还是没运行到Timer1Timer中去??
    在Form1中用的方式:procedure TForm1.FormCreate(Sender: TObject);
    begin
        obj:=Tobj.create(self);   //创建Obj对象,按道理Obj.timer会自动运行,可是没有?
        obj.oncal:=self.oncal;    // 这里赋给TObj.oncal
    end;
    en.. 估计能认真看懂的也不多,我也只能说得这样清楚了 唉。。
      

  4.   

    1.加入一个专用的timer,
    2.对此timer设定时间,
    3.用此timer的ontimer事件就可作为你的onCal()事件,==谢谢 zzwu(未名) 
    你的意思应该是在Form1中加入Timer来当作Oncal,这不是我要的。原来那个FonCal(self)可以用的我现在就是 不能在Obj.Timer的Ontimer中执行Timer1Timer程序!
      

  5.   

    procedure要靠事件来触发,我看不出你的程序中最原始的事件是靠什么来产生的?
    这里的"原始事件"是指程序运行之后所发生的某种事件,如鼠标点击,键盘按下,timer定时。
    FormCreate虽然也是一个事件,但它只发生一次,在程序运行之后就不会再发生。
      

  6.   

    To zzwu(未名) 先谢谢,不过我的照你说的也没错啊。
    1.在FromCreate中实现Tobj.create即实例Obj对象2.TObj.create中我才实现TTimer.create,建立Timer对象3.在Timer.ontimer中才触发属性事件 FonCal(self); 没错啊,
    我看不出你的程序中最原始的事件是靠什么来产生的?
    ==我是用Timer来产生的 可是不明为什么TObj中的Timer的Ontimer不能触发??