1214 Votes

Delphi: Event Order of a Form - From OnCreate to OnDestroy

Info by Stefan Trost | Last update on 2022-11-02 | Created on 2013-03-29

In Delphi, every form passes through a couple of events in a specific order when opening and when closing. In this info, I would like to introduce these events and I would like to show you in which order and at which point they are carried out.

Creating the Form

When creating the Form, the following events are carried out in the following order. The later events of this list are even called in some cases during the program execution.

  1. OnCreate: The OnCreate event is only called once. That is when creating the appropriate Form. In most cases, this is done automatically directly after the start of the program, when all Forms of the application are created by default. The OnCreate event can be used, for example, for initializing variables which should be used during the whole program execution. This variables should be freed in the OnDestory event.
  2. OnShow: With the OnShow event, a Form becomes visible. OnShow follows the OnCreate event and it is also executed during the program execution, for example whenever setting "visible" of a Form to true or using Form.Show in order to make an invisible Form visible.
  3. OnActivate: OnActivate is called whenever the corresponding Form gets the focus. That is when first another window is activated and the user clicks on the window. If an application has two windows and the user is switching between these windows, at each change the OnActivate event of the appropriate Form is executed. In OnActivate, you can, for example, set the focus to a specific element manually.
  4. OnPaint: Is executed whenever the Form has to be painted, for example after another window has covered the Form. In this event, you can paint own things on the Form, for instance.
  5. OnResize: OnResize is executed whenever changing the size of the Form. If you should have some controls which should change their size according to the size of the Form (for example the left or top position), you should write the code for that into the OnResize event.

Closing the Form

When closing the window or when finally destroying the form, the following events are called in the following order:

  1. OnCloseQuery: When trying to close a form (for example in the case the user clicks on the red x), the OnCloseQuery event will be executed even before the window will be closed. In this event, it is still possible to prevent the closing of the Form by setting the variable "CanClose" to "false". For instance, you can show a dialog box in OnCloseQuery in which the user has to confirm if he really wants to close the program of if he would like to save possible changes: if MessageDlg('Close without saving?', mtConfirmation, [mbOk, mbCancel], 0) = mrCancel then CanClose := false; If the user clicks on Cancel, the following events will not be executed and the user comes back to the program window.
  2. OnClose: If closing has not been prevented in the OnCloseQuery event, the OnClose event will be called. Also in this event, there is a variable named "Action" of the type "TCloseAction" which can be used for manipulating. If you set this variable to "caNone", you can prevent the closing of the form again. Other possibilities are "caMinimize" to minimize the form instead of closing, "caHide" to set the Form to invisible instead or "caFree" to free the Form.
  3. OnDeactivate: At this point, the Form looses the focus.
  4. OnHide: At this point, the Form becomes invisible.
  5. OnDestroy: Here the Form is finally closed and freed. If it is the MainForm, the whole application is closed. All variables you have initialized in OnCreate, you can free at this point.

ReplyPositiveNegative
00 Votes

Concerning this topic I have the following remarks:

Lazarus:

  • TForm.OnCreate
    Form.Visible=false and Form is invisible, Form remains invisible even after Form.Show; followed by Application.ProcessMessages;
  • TForm.OnShow
    Form.Visible=true, but Form remains invisible despite Form.Visible=true and remains unchanged even after Application.ProcessMessages;
  • TForm.OnActivate
    Form.Visible=true and Form is finally visible

Delphi:

  • TForm.OnCreate
    Form.Visible=false and Form is invisible, however, Form becomes visible already within the OnCreate routine after Form.Show; followed by Application.ProcessMessages; as well as automatically after finishing TForm.OnCreate
  • TForm.OnShow
    Form.Visible=true and Form is visible
  • TForm.OnActivate
    Form.Visible=true and Form is visible

Clarification concerning OnActivate: TForm.OnActivate will always be invoked if the corresponding form becomes focused for the first time, i.e. after OnCreate and after the subsequent first call of OnShow.

If an application has more than one single form and the user later switches focus between any of these forms, the OnActivate event of the form of the same application becoming focused will be triggered.

If, however, a different application is initially active and the user then clicks the form of another application, it is not the OnActivate routine of the clicked form that will be called in this case but, instead, the OnActivate event Application.OnActivate of the Application object belonging to the clicked form will be triggered.
2017-11-01 at 12:57

ReplyPositive Negative
Reply

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.