Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assign a procedure to a TNotifyevent

1,912 views
Skip to first unread message

Andreas Seebeck

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to
Hi,
just wanted to create a TTimer at runtime but ran into the problem that I
cannot simply write a procedure ontimerproc(sender: TObject); and assign it
to the ontimer event like mytimer.ontimer:=ontimerproc;

Does anybody know how this must be done?

Thanks for any help,

Andreas Seebeck

Finn Tolderlund

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to

"Andreas Seebeck" <A...@febe.com> skrev i en meddelelse
news:8uejq9$5k...@bornews.inprise.com...

> just wanted to create a TTimer at runtime but ran into the problem that I
> cannot simply write a procedure ontimerproc(sender: TObject); and assign
it
> to the ontimer event like mytimer.ontimer:=ontimerproc;
> Does anybody know how this must be done?

The timer event expects an *object* procedure.
So make the procedure into a method on your form, like this:
procedure TForm1.OnTimerProc(Sender: TObject);
Then you can write
mytimer.OnTimer := OnTimerProc;

Finn Tolderlund


Guido Gybels

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to
Andreas Seebeck wrote in message <8uejq9$5k...@bornews.inprise.com>...

>just wanted to create a TTimer at runtime but ran into the problem that I
>cannot simply write a procedure ontimerproc(sender: TObject); and assign it
>to the ontimer event like mytimer.ontimer:=ontimerproc;


Look at the declaration of TNotifyEvent: it is <procedure ... of object>, so
you will need to create a method in stead of a stand alone procedure:

procedure MyObject.OnTimerProc(Sender: TObject);

Guido GYBELS
Programmer (GDG GROEP BELGIUM)


Thomas Minx

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to
Design a TTimer (Timer1);
click on OnTimer event (Timer1Timer);
modify it;
delete the timer from the form;
then use the procedure Timer1Timer:
MyTimer:=TTimer.Create(Self);
MyTimer.OnTimer:=Timer1Timer;

Thomas

Andreas Seebeck <A...@febe.com> schrieb im Beitrag
<8uejq9$5k...@bornews.inprise.com>...
> Hi,


> just wanted to create a TTimer at runtime but ran into the problem that I
> cannot simply write a procedure ontimerproc(sender: TObject); and assign
it
> to the ontimer event like mytimer.ontimer:=ontimerproc;
>

> Does anybody know how this must be done?
>

Peter Below (TeamB)

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to
In article <8uejq9$5k...@bornews.inprise.com>, Andreas Seebeck wrote:
> just wanted to create a TTimer at runtime but ran into the problem that I
> cannot simply write a procedure ontimerproc(sender: TObject); and assign it
> to the ontimer event like mytimer.ontimer:=ontimerproc;

You can, but the procedure has to be a method of some object, e.g. the form.
Methods have an additional hidden parameter (Self), so they are not
compatible with a standalone procedure that seem to have the same parameter
list.

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


Bruce Roberts

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to

"Andreas Seebeck" <A...@febe.com> wrote in message
news:8uejq9$5k...@bornews.inprise.com...
> Hi,

> just wanted to create a TTimer at runtime but ran into the problem that I
> cannot simply write a procedure ontimerproc(sender: TObject); and assign
it
> to the ontimer event like mytimer.ontimer:=ontimerproc;
>
> Does anybody know how this must be done?

One should really declare the procedure as method of some object. However,
if that is not possible the following work around exists.

procedure HandleTick (Sender : tObject); forward;
const HandleTickHack : tMethod = (code : @HandleTick; data : nil);

. . .
myTimer.OnTimer := tNotifyEvent (HandleTickHack);


Its not really necessary to use a forward declaration but IMO it makes
things a bit clearer.

Andreas Seebeck

unread,
Nov 10, 2000, 3:00:00 AM11/10/00
to
> You can, but the procedure has to be a method of some object

I tried this, so now it IS a method of some object. Here is the decl.:
type TIAni = class(TRBob)
procedure onTimerproc(Sender: TObject);
protected
private
public
end;

but mytimer.ontimer:=TIAni.ontimerproc; gives me the comp. error
[Error] Unit1.pas(164): Incompatible Types: 'TNotifyEvent' and 'Procedure'

Any hints?

Thank you very much for all the help!

best regards,

Andreas Seebeck


>
> Peter Below (TeamB) 10011...@compuserve.com)
> No e-mail responses, please, unless explicitly requested!
>

I'm very sorry, I hit the wrong button.

Guido Gybels

unread,
Nov 10, 2000, 3:00:00 AM11/10/00
to
Andreas,

Without more code, I can only guess, but you obviously are doing something
wrong, probably while declaring the procedure. Let's assume you have a form
TForm1 and a timer, Timer1. Declare a method for the form by adding a line
to the private declarations of the form:

procedure MyEventProc(Sender: TObject);

Afterwards, you can simply assign this procedure to the OnTimer event:

Timer1.OnTimer:=MyEventProc;

Hope this helps,

Stan Stern

unread,
Nov 10, 2000, 3:00:00 AM11/10/00
to
Andreas,

I had exactly the same problem some time ago and managed to solve it using a
"Class Method" inside a dummy class. "Class methods" don't require you to
instantiate the object defined by the dummy class before calling them.
Instead (I believe), you simply call a class method with a class prefix.

So, instead of OnClick := Click;
you would write OnClick := TDummyClass.Click;

I'd love to show you an exact example, but it's on my system at home. Try
looking up Class Methods in the Delphi help. Maybe someone else here can
explain this approach better than I can.

Best of luck


Regards

Stan Stern.

Peter Below (TeamB)

unread,
Nov 10, 2000, 3:00:00 AM11/10/00
to
In article <8ugcrc$nk...@bornews.inprise.com>, Andreas Seebeck wrote:
> I tried this, so now it IS a method of some object. Here is the decl.:
> type TIAni = class(TRBob)
> procedure onTimerproc(Sender: TObject);
> protected
> private
> public
> end;
>
> but mytimer.ontimer:=TIAni.ontimerproc; gives me the comp. error
> [Error] Unit1.pas(164): Incompatible Types: 'TNotifyEvent' and 'Procedure'

Well, i said "method of an object", you are trying to use the *class* itself
here, not an instance of it (a object). The error message is somewhat
misleading, unfortunately.

Var
aTIAni: TIAni;

aTIAni := TIAni.Create;
mytimer.OnTimer := aTIAni.OnTimerproc;

The aTIAni object has to live at least as long as the timer object is tied
to its method.

0 new messages