Technology Programming

Implementing the OnBeforeCreate and OnAfterCreate 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 formsend; 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 executedend;

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.DoCreate(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 methodend; 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.

Delphi tips navigator:
» Specify Custom Icon for a Console Mode Delphi Application
« MDI Child Count By Class - Count MDI Children By Child Class Name in Delphi MDI Applications

SHARE
RELATED POSTS on "Technology"
Blitzerwarner: Could You Escape the Speed Cameras?
Blitzerwarner: Could You Escape the Speed Cameras?
What Makes a Perfect Web Design Company?
What Makes a Perfect Web Design Company?
Inside No-Fuss Products In Gaming Computers
Inside No-Fuss Products In Gaming Computers
Gaming Institute, Game Design Schools at PAI-ILS, PAI International Learning Solutions Pune
Gaming Institute, Game Design Schools at PAI-ILS, PAI International Learning Solutions Pune
Free Music Players For Websites
Free Music Players For Websites
'See How NCP Tracks Down Fake Ration Card Holders'
'See How NCP Tracks Down Fake Ration Card Holders'
You Can Stop Panic Attacks in Just two Minutes
You Can Stop Panic Attacks in Just two Minutes
WordPress Blog to WP Site! A Journey to Online Success
WordPress Blog to WP Site! A Journey to Online Success
Videos will give your business portal an extra leverage
Videos will give your business portal an extra leverage
Excellent Ways To Make Weight Loss Work For You
Excellent Ways To Make Weight Loss Work For You
Five Notable Differences between National and International Logo Designs
Five Notable Differences between National and International Logo Designs
Who Should Your Phoenix Web Design Client Be?
Who Should Your Phoenix Web Design Client Be?
White Papers & Other Documents - including ALFLB
White Papers & Other Documents - including ALFLB
Google Analytics Tips
Google Analytics Tips
Want To Make Your Website Faster? Follow These Steps
Want To Make Your Website Faster? Follow These Steps
The Starting of Couture Apparel
The Starting of Couture Apparel
Choose Custom Website Design Services
Choose Custom Website Design Services
Choosing the Right Joomla CMS Developer
Choosing the Right Joomla CMS Developer
A Look at SQL Server Denali AlwaysOn
A Look at SQL Server Denali AlwaysOn
4 Tips to Choose the Perfect Flash Animation Course
4 Tips to Choose the Perfect Flash Animation Course

Leave Your Reply

*