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

AfterConstruction, or when to call OnCreateEvent

186 views
Skip to first unread message

Ruslan Popov

unread,
Nov 5, 2000, 3:00:00 AM11/5/00
to
Hello,

I overrided the AfterConstruction method in my component in order to call
OnCreate event handler if assigned. However, I found that AfterConstruction
is called BEFORE the component has been loaded from the stream. Thus, the
component's properties, including the event handler pointer, are not
assigned yet, and I cannot call the handler if any. Why so? Do I need to do
call OnCreate in Loaded method instead? The latter would not seem logical.

Thanks,
Ruslan

Peter Below (TeamB)

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

You have discovered the reason why TFrames don't have an OnCreate event,
there is simply no good place to fire it from. For a form the event can be
fired from Afterconstruction since it is the constructor of the form that
does the loading from the DFM resource. For a component that is dropped on
the form, however, the loading happens after the constructor has returned
(the component is created by the streaming system), or it does not happen at
all (when the component is dropped on the form in the designer). Call your
event "AfterLoaded" and fire it from an overriden Loaded method, get used to
the fact that the event may fire more than once (unless you code to guard
against that) since Loaded may be called more than once if the component is
used on a form with visual form inheritance. And it will not be called at all
in the designer when the component is dropped on the form the first time.

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


Ruslan Popov

unread,
Nov 7, 2000, 6:30:55 PM11/7/00
to
Ok, Peter, I see. Thank you. ..... So, you suggest Loaded is the best place
to fire the event? Is it possible somehow to prevent recurring calls in case
of visual inheritance? (this is exactly what I do; sure, it's a Merphy law)
May be there's some state flag, or compare Form's ClassName in order to
distinguish inherited forms' Loaded methods?

Best regards,
Ruslan


Werner Lehmann

unread,
Nov 8, 2000, 3:00:00 AM11/8/00
to
Ruslan Popov wrote:
>
> to fire the event? Is it possible somehow to prevent recurring calls in case
> of visual inheritance? (this is exactly what I do; sure, it's a Merphy law)

Call this proc from the overrided Loaded:

procedure Txyz.DoAfterLoaded;
const FirstTime: boolean = true;
begin
if not FirstTime then exit;
FirstTime:=false;
if assigned(fOnAfterLoaded) then
fOnAfterLoaded(self);
end;

WL


Lothar Böhler

unread,
Nov 9, 2000, 2:42:46 AM11/9/00
to
Werner Lehmann <w...@bwl.uni-kiel.de> wrote in
3A08AB2E...@bwl.uni-kiel.de...

> Ruslan Popov wrote:
> >
> > to fire the event? Is it possible somehow to prevent recurring calls in
case
> > of visual inheritance? (this is exactly what I do; sure, it's a Merphy
law)
>
> Call this proc from the overrided Loaded:
>
> procedure Txyz.DoAfterLoaded;
> const FirstTime: boolean = true;
> begin
> if not FirstTime then exit;
> FirstTime:=false;
> if assigned(fOnAfterLoaded) then
> fOnAfterLoaded(self);
> end;
>
> WL
>

No! If "FirstTime" is "static" variable (to say it in C-slang) the event
what only be called once for the whole program even if there were serveral
of the components.
You could Make FirstTime an instance variable. But then the event would be
called after the first load not after the last!?
A workaround could be to:
- Make FirstTime an instance variable.
- Call the event in the Paint-Method.
The Paint-Method would be surely late enough for this.

Another (better) workaround:
- Make FirstTime an instance variable.
-In the constructor send a message to your component or a invisible window
created with AllocateHWnd.
-When the message arives the loading is done. (Did not test, but im 99%
sure).

If you succeed (or not) please let us now.

Lothar Böhler


rb

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

"Werner Lehmann" <w...@bwl.uni-kiel.de> wrote in message
news:3A08AB2E...@bwl.uni-kiel.de...

> Ruslan Popov wrote:
> >
> > to fire the event? Is it possible somehow to prevent recurring calls in
case
> > of visual inheritance? (this is exactly what I do; sure, it's a Merphy
law)
>
> Call this proc from the overrided Loaded:
>
> procedure Txyz.DoAfterLoaded;
> const FirstTime: boolean = true;
> begin
> if not FirstTime then exit;
> FirstTime:=false;
You can't change value of a constant. This shouldn't work.
Without seeing a whole thread, I'd say Ruslan probably needs either a
singleton or a class method.

rb

Werner Lehmann

unread,
Nov 10, 2000, 7:42:30 PM11/10/00
to
rb wrote:
>
> You can't change value of a constant. This shouldn't work.
> Without seeing a whole thread, I'd say Ruslan probably needs either a
> singleton or a class method.

Changing a const is possible. It is rather static but constant.

And Lothar is right that this will only fire for the first Loaded
occurance. I don't know if classes share their local const vars
but probably Lothar is right here as well.

WL


rb

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

Damn, I feel ashamed. In my language const means const - not static!!!

TAB.

rb

"Werner Lehmann" <w...@bwl.uni-kiel.de> wrote in message

news:3A0C95F6...@bwl.uni-kiel.de...

Ruslan Popov

unread,
Nov 12, 2000, 3:00:00 AM11/12/00
to
Hello,

> Another (better) workaround:
> - Make FirstTime an instance variable.
> -In the constructor send a message to your component or a invisible window
> created with AllocateHWnd.
> -When the message arives the loading is done. (Did not test, but im 99%
> sure).
>
> If you succeed (or not) please let us now.

I believe this would work just fine, but my component does not have a
window.

Regards, and thanks for help!
Ruslan

Ruslan Popov

unread,
Nov 14, 2000, 3:00:00 AM11/14/00
to
I cannot detect recurring calls when debugging the component in Delphi.

0 new messages