The Unofficial Newsletter of Delphi Users - by Robert Vivrette




Piggybacking Event Handlers with Components

by Stein Svendsen - stein.svendsen@idgruppen.no

I have enjoyed UNDU some time now, getting many hints and ideas from it. Reading a past issue, Alan Lloyd pointed out the way to change event handlers at runtime, I realized that I could clean out some of my custom forms in the repository.

In my repository growing full of different forms, I have quite a few that have the OnCreate event filled up with code. These forms don't hide my customization when inherited, so they all have plenty of code already in the OnCreate handler when I use one. I prefer to have my forms as clean and codeless as possible, with all event handlers free and fresh for use.

I found that i could move my customizations into components that chain into a form's event handlers. The example below shows an example of this.

The component below may be dropped onto a form, and will make the form materialize itself with a feathery effect when created. The Speed property determines how quickly the effect runs. But the main thing is that it won't interfere with any OnCreate code that the form may have from before.

As far as I can discover, a form, when created,  will create all its objects first, paint itself, and then fire its OnCreate code (if any). This component's create() code maps the form's Oncreate event to it's own FormOncreateIntercept procedure. Hence, when the form is ready to execute it's OnCreate code it actually executes the code that the component's FormOncreateIntercept code. The FormOncreateIntercept will execute the form's original OnCreate event if there was one, allowing any other initializations to be completed, before it runs it's own.

(I imagine that this technique allows multiple components using it,  to operate on the same form as long as they are different from each other, and don't conflict in their nature.)

Note: You can't use this component with a modal form.

Editors Comment: This is a pretty neat application of this technique. The function of the Speed property will differ though depending on how fast the machine is that is running the code. I am sure a Timer would probably make this work regardless of processor speed.