0

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.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
  • 1
    You could use interceptor classes, but although that would not make you edit all forms, it would make you edit all units to add the interceptor class unit to the uses clause. Depending on exactly how your code is structured this may or may not be an issue. – Dsm Jul 16 '18 at 09:05
  • Thanks, I was not aware of this, it is for sure a third item for my list, but as you say it does not save me from editing all forms Unfortunately the application is quite old and I did not use a base form to inherit from at that time, while i do this in all my recent apps since it is convenient. – UnDiUdin Jul 16 '18 at 09:11
  • 9
    Events aren't the way to do this. What you need is a base form from which all forms are derived. You then override the constructor of that form and place the common code there. – David Heffernan Jul 16 '18 at 09:15
  • How many units are we talking? If there are enough it might even be worth writing a little program to do the job for you. If you were to do this I would recommend the interceptor approach because modifying the uses clause would be easy. Other options are doable but harder. – Dsm Jul 16 '18 at 09:26
  • 4
    I would opt for the base form approach, even if it needs to touch all forms, because it can be reused for similar purposes in the future. – Uwe Raabe Jul 16 '18 at 09:41
  • i see base form is the way to go. It is not impossible to do since there are about 50 forms, it is just a simple job. But I wanted to know if there were some techniques i was not aware of. – UnDiUdin Jul 16 '18 at 09:48
  • 1
    Wouldn't it be possible to hook T(Custom)Form.Create? – dummzeuch Jul 16 '18 at 12:12

1 Answers1

1

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

UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
  • Personally I think this is the best way to do it. But as a comment, if the event handler for FormCreate doesn't change on descendant forms you can safely remove it. Just remove the inherited call in the event handler and save your form which should remove the event handler form the descendant. Since you won't have the event handler in descendant forms the one on the ancestor / base form will be used. But ... having it explicitly in every descendant form is not wrong of course ... – Stefaan Jul 16 '18 at 12:33
  • 2
    This might be more a preference by me than a rule in Delphi - maybe because I've had on more than one occasion bad experience with exactly the case - , but I'm trying to avoid using the events of a type during its implementation. What I mean is that for the implementation of TIWAppForm I wouldn't use its event OnFormCreate - I'd override the virtual DoFormCreate instead - because events can be assigned by the consumer of this type, destroying implementation details. – nil Jul 16 '18 at 13:12
  • 2
    Events are the wrong way. Override the constructor. -1 – David Heffernan Jul 16 '18 at 21:43
  • Are you sure that your answer is about doing it "without editing all forms?" You defined "editing a form" in your question, but in the answer you do it by adding the code to the event handler "of all forms". How to do it "without editing"? What is the difference between your solution and simple "find and replace" thru all the sources for the forms? ;) And please - dont get me wrong - the discussion here is about adding the code for all forms in the elegant and convenient manner, but not "without editing". IMHO the solution (for the actual question) should utilize RTTI or something like that. – zdzichs Jul 17 '18 at 07:42