Forms Delphi

Title: Implementing the On-AFTER-Create And On-BEFORE-Create Events for Delphi Forms
The TForm's OnCreate event is called when the form is created. Usually, you would write an OnCreate event handler to perform special processing when the form is created - such as setting any startup information (initial control property values, for example) for form operation.
When a form is created (with the Visible property set to true) the order of the events is as follows: OnCreate, OnShow, OnActivate, ...
If you are using visual form inheritance to have forms that inherit from some "base" forms, you might need to execute custom "startup" code for each inherited form.
Here's an example:
type
TBaseForm = class(TForm)
procedure FormCreate(Sender: TObject) ;

....

procedure TBaseForm.FormCreate(Sender: TObject) ;
begin
// common OnCreate "startup" code for
// Base Form and all inherited forms
end;

The TBaseForm is a base class for some of the forms in the project - it contains common visual (user interface) elements and common event handlers for those elements.
The OnCreate event handler has "startup" code for all instances of TBaseForm.
Now, here's another form, TComplexForm, that inherits from TBaseForm.
type
TComplexForm = class(TBaseForm)
procedure FormCreate(Sender: TObject) ;

....

procedure TComplexForm.FormCreate(Sender: TObject) ;
begin
some code here BEFORE TBaseForm's OnCreate is executed

inherited; //calls TBaseForm's OnCreate

some code here AFTER TBaseForm's OnCreate is executed
end;


The OnCreate event handler for TComplexForm can execute some code BEFORE and AFTER the code in the inherited TBaseForm's OnCreate event handler is executed.
On After Create?
What if you need to put some common code in the TBaseForm implementation that needs to be executed when all the code in the OnCreate event handlers is executed?
You can not place it in the TBaseForm.OnCreate event handler, as it could be called before TComplexForm.OnCreate has finished!
You need an OnAfterCreate event handler that you would use in the TBaseForm implementation - this code would be executed after the creation of any inherited form.
The solution is in overriding the protected virtual DoCreate procedure of the TCustomForm class.
Note that your forms will inherit from TForm that inherits from TCustomForm - therefore you can override virtual methods of the TCustomForm class (defined in the forms.pas unit).
The DoCreate actually calls the OnCreate event handler if it has been specified.
type
TBaseForm = class(TForm)
protected
procedure DoCreate() ; override;

....

procedure TBaseForm.FormCreate(Sender: TObject) ;
begin
some code here BEFORE TBaseForm's OnCreate is executed

inherited; //calls TBaseForm's DoCrete - raises the OnCreate event

some code here AFTER TBaseForm's OnCreate is executed

AfterCreate; //your custom OnAfterCreate method
end;

That's it. The code you place AFTER the "inherited;" line above, procedure named "AfterCreate", will get executed when the OnCreate event handler for all inherited forms has been run.
That is, when TComplexForm.OnCreate is finished, our "AfterCreate" will be called.
Note that the above solution also provides a way to have an OnBeforeCreate common code.