Search code examples
delphidelphi-10-seattle

How to smartly add a line of code in all the FormCreate events without editing all forms?


I need the call an Initialization routine in all the forms of my application, it is an Intraweb application so in fact I will use OnIWAppFormCreate and not OnFormCreate, anyway the problem is the same.

I have 2 techniques in mind:

  1. Use Visual Form Inheritance: in this case I create a BaseForm in which I implement the FormCreate event and in inherit all forms from this

  2. I add a global procedure and I call it from all forms

Both these tecnhiques make me edit each form.

Is there a way to avoid this?

In fact what I need to do is to add the same string to the TIWAppForm.ExtraHeader stringlist property.

This is the code i need to add to each form:

ExtraHeader.Add('<link rel="stylesheet" type="text/css" href="/css/mycustomcss.css">');

Somehow I'd like to know if there is a way to code this only once with a tecnhique I am not aware of.

Thanks.


Solution

  • The best solution is to inherit from a base form, even because in almost all my forms i coded the OnFormCreate so to implement my task i did:

    1. create a base form inheriting from TIWAppForm

    2. in the unit declaration i inherit from the base form

    3. in the dfm i replace object with inherited

    4. in the FormCreate of all forms i call inherited explicitly