Need to access some components right after program start but I have found that doing so from form's onCreate event is bad because at the moment they may still be unavailable (access violation occurs). Can not find onCreate event in any component. Am I missing something?
CodePudding user response:
Use the form's onShow() -event.
But be aware, that the onShow() -event is called every time the form is shown, not only the first time.
CodePudding user response:
In your form, you should override the DoShow
method and insert your code there. Since DoShow
is called everytime the form changes from invisible to visible, you should also add a boolean variable in your form and check in the DoShow
override, if it is false
. In that case, it is the first time DoShow
is called. Then set the variable to true
and do whatever you need to do the first time.
Please note that within DoShow
the form is not visible yet. If you need to do something once the form is made visible, you can post a custom message from DoShow
and put your code in the corresponding message handler. At the time it is executed, the form just became visible.
const
WM_APP_STARTUP = WM_USER 1;
type
TForm1 = class(TForm)
protected
FInitialized : Boolean;
procedure DoShow; override;
procedure WMAppStartup(var Msg: TMessage); message WM_APP_STARTUP;
end;
implementation
procedure TForm1.DoShow;
begin
// Form is NOT visible but will soon be
if not FInitialized then begin
FInitialized := TRUE;
// Insert your code here
PostMessage(Handle, WM_APP_STARTUP, 0, 0);
end;
inherited DoShow;
end;
procedure TForm1.WMAppStartup(var Msg: TMessage);
begin
// The form is now visible
// Insert your code here
end;