delphijedi

onCreate is not the first event


I use a form with a JVWizard from Jedi (version 3.50) on it. I want to do some initialization stuff in the oncreate event, because I thought it will be the first event after creation of the form (like described here). But i found out that the onenterpage event of the welcomepage (first page in page list) event is fired before the oncreate event. I'm wondering what is wrong.


Solution

  • The sequence of events for a form are not absolutely guaranteed. Or rather, the events of a form are determined by the form, but this does not guarantee anything with respect to events triggered by other components.

    The OnCreate event of a form is called once the form has been constructed, after any and all components and controls on that form have themselves been initialised.

    If those components or controls trigger any events then these may occur before the form OnCreate event itself.

    In your case, since you are using the JvWizard components and controls then if you have these controls and components placed on your form at design time then any events triggered by their initialization as they are loaded and initialised at runtime will occur before the Form.OnCreate event.

    The OnEnterPage event is one of these, triggered by the JvWizard as it initialises and establishes its first page.

    Without knowing the details of precisely what initialization it is you are trying to perform it is impossible to say what the correct solution in your case might be.

    It might be simply to perform your form initialization later, for example in response to the forms OnShow event.

    Or it may be to move some (or all) of the initialization to the wizard OnEnterPage itself.

    Or it may be appropriate to instead implement your initialization as an override of the virtual Create constructor.

    Overriding the constructor itself would enable you to perform some initialization before calling the inherited constructor (to initialise the form contents and eventually call OnCreate) and some initialization after the call to inherited (which will run after any Form OnCreate event handler).

    constructor TMyForm.Create(Owner: TComponent);
    begin
      // Perform initialization BEFORE calling inherited and 
      //  BEFORE any components or controls have initialised and
      //  triggered any of their events.
      //
      // But remember:  At this point there are no form contents loaded!
      //  As a result the amount of useful initialization you can do
      //  here may be limited.
    
      // ..
    
      inherited Create(Owner);
    
      // Perform further initialization here ...
      //
      // At this point form contents (controls/components) have been
      //  loaded and any events have been triggered and handled,
      //  including FormCreate.  Any code here might even be better
      //  left to run in the FormCreate event handler.
    end;
    

    Hopefully with an understanding of what is going on you will be able to identify the appropriate approach in your case.