Re: Event handlers...
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 27 Jan 2006 17:12:38 +0100
"swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1138369863.114264.43660@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> [...] Can the event handler belong to a different class?
Yes. The implicit Self parameter (the "context object") will be an
instance of that class.
This is why all event handlers have Sender as their first parameter,
and why it is a TObject, not anything more specific. Because Self
is already taken for a different context object.
Event handlers effectively have two context objects: the one that
fires the event (Sender) and the one that catches it (Self). The
latter is usually the _less_ interesting of the two.
> What if I had a data module to hold event handlers that I wanted to
> use across many classes (like validating alphanumeric keypresses,
> making "enter" act like a "tab" (don't ask!), etc) Can I do something
> like that?
Yes. Not even necessarily a datamodule, it can be any class. But there
are dragons there. Delphi doesn't go out of its way to make that easy
(although it turns out to be easy after all).
"swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1138370253.380841.61250@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[...]
> Does this mean, then, that an event handler has a "self" variable?
> What if the event handler is a class procedure of a different class,
> when I call it, what is in the "self" variable? (Is it even possible
> to have the event handler be a class procedure?)
All the usual rules apply. If it's a method, it has a context object.
The context will be an instance of the class the method is declared in.
(Subject to polymorphism, as always.)
It _is_ possible to use a class method as an event handler. I haven't
seen it documented anywhere, but it works.
It's also possible to use a function as an event handler (for an
appropriately typed event). Has anyone ever done that? Without the
urge to throw up?
> How would I set up a class to use an event handler defined in a
> different class? I have some kinds of events which I want handled in
> the same way across multiple forms, like validating keypresses...
You set up an _instance_ to use an event handler.
Sender.OnEvent:=Receiver.Handler;
If a sender or receiver is Self, you can leave it out as normal.
Receiver becomes Self later. Its value, _together_ with a pointer
to Handler, makes up the method pointer stored in the OnEvent
property.
The handler may be static or virtual, a static address is resolved at
compile time and a virtual address at runtime. All as usual. The
declared type of Receiver is used to resolve a static Handler, so
in that case you _do not need an instance_. A dummy variable or even
casting nil to the right classtype will work.
But that either requires an unused variable or an ugly cast, so my
"library event handlers" tend to be class methods. Their context is
not an instance but a class reference. Either a literal ("TForm1")
or a classtype reference in a variable will do. Again, variables with
non-null values are required for virtual methods. Yes, class methods
can be virtual, too - in classic Object Pascal, not in .Net.
A final option is to declare a regular procedure with an extra Self
parameter and the register calling convention, and construct a method
pointer with any reference for the context object and that procedure
as the entry point. I've done it in a case where I was writing code
in the project file and did not have a class at my disposal at all
to put the event handler in. It's not something I would recommend
otherwise.
One thing you can't do: set an event handler that is a class method
in the Object Inspector (tried on D6). You have to do it in code.
I must say that these are all excellent questions, and that some
experimentation could also have answered all of them.
Groetjes,
Maarten Wiltink
.
- Follow-Ups:
- Re: Event handlers...
- From: swansnow
- Re: Event handlers...
- References:
- Event handlers...
- From: swansnow
- Re: Event handlers...
- From: Rob Kennedy
- Re: Event handlers...
- From: alanglloyd@xxxxxxx
- Re: Event handlers...
- From: swansnow
- Event handlers...
- Prev by Date: Re: Recommendations for a text to speech component?
- Next by Date: Re: DBGrid1.OnDrawColumnCell goes into infinite loop
- Previous by thread: Re: Event handlers...
- Next by thread: Re: Event handlers...
- Index(es):
Relevant Pages
|