The execution order of @AfterCreate and WebFormCreate.

When I create a child form, I use the next structure:

procedure TForm1.WebButton2Click(Sender: TObject);
   procedure AfterCreate(AForm: TObject);
      TForm3(AForm).OnSaveData := DelegateSaveData;  {<<<<<<<====== Delegate assigment}
      ShowMessage('I''m Assigning the Delegate');
   FCurrentChildForm := TForm3.CreateNew(PanelContainer.ElementID, @AfterCreate);

What is expected is that the method WebFormCreate (OnCreate event Handler) of the TForm3 instance was executed just before the @AfterCreate procedure of the caller, don't true?

It happens in the other order: First is executed the AfterCreate and after the OnCreate.

I add a little project as an example.

It's confusing because I want to check if the event handler is nil to send the programmer an advertisement about the need to assign the Delegate, and I can't. (9.4 KB)

1 Like

Please understand that the current particular order was chosen and we cannot change this as this could potentially break other users applications that rely on this sequence.

Yes, of course, but it is a failure, isn't it true?

It must be more straightforward and documented because it is anti-logical.

What is the finality of @AfterCreate if it doesn't await the creation of the form?

Both OnCreate event handler and AfterCreate proc are two ways that allow to do something after form creation. AfterCreate is called after the form is created.

Yes, of course. The meaning is there, but

Let's clarify the sequence of events: when do you expect the OnCreate event to occur? It's part of the form creation process, specifically in the final stage.

When you call the Create constructor, when do you expect the delegate @AfterCreate to occur? After the form creation, including his OnCreate Event Handler execution?

OnCreate is in the final stage of form creation (after form instance is created, DFM data is loaded)
@AfterCreate is called after this

This is not correct. You can also see the project I've uploaded.

First is called @AfterCreate and then the OnWebCreate (OnCreate).

Please execute and see the ShowMessages for the example project.

We have seen one place where this could be the case and have fixed this.
Next update will have this fix.

Pleas be very careful with these changes as I use this all of the time in its current form and it works fine for me and am taking advantage of how it works now!

1 Like

Are you using the form's OnCreate event AND the anonymous AfterCreated method from CreateNew() AND rely on the sequence of these 2 calls?


Despite me asking you not to do this you have now broken my web app in 2.5.4.

Sorry. We had to do what is technically correct.

Surely you should never break baxkwards compatibility? You should have added an option or at least added a BeforeCreate event.

How can I reverse this?

I think the solution is easy:
You must move the needed code of WebFormCreate( -OnCreate- event handler method to WebFormDOMContentLoaded( -OnDOMContentLoaded method.

This method is called after the @AterCreate delegate and after the OnCreate event in the form.

And I think your code will be more precise, and easy to maintain.

Thanks but the OnDomContentLoaded event is not being calledin my case.

OK, i got round this by overriding the CreateNew constructor passing an additional argument to it which is then used in the OnCreate event.