Delphi: Event Order of a Form - From OnCreate to OnDestroy
Info by Delphian | Last update on 2021-04-06 | 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.
- 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.
- 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.
- 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.
- 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.
- 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:
- 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.
- 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.
- OnDeactivate: At this point, the Form looses the focus.
- OnHide: At this point, the Form becomes invisible.
- 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.
About the Author
The author has not added a profile short description yet.
Tip | 1 Comment
Question | 2 Answers
Question | 1 Answer
Question | 4 Answers
Tutorial | 0 Comments
Tip | 0 Comments
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.
Ask your own question or write your own article on askingbox.com. That’s how it’s done.