Sign in Join
Wiki copytemporary

Event order: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Overview
22
33Lazarus offers various events that you can use to enter your own procedures to handle things that happen in your application (e.g. a user clicks a button).
44
55There are rather a lot of possible events to cater for a lot of different scenarios. Somebody who does not know Lazarus or Delphi could well pick the wrong event.
66
77## Lazarus documentation
88
99The [[Lazarus Tutorial|Lazarus tutorial]] has some information on what events do what.
1010
1111## [[TForm|Forms]]
1212
1313Typical event order for [[TForm|forms]] is:
1414
15-`OnCreate => OnShow => OnActivate => OnPaint => OnResize => OnPaint => ... `\
16-`OnCloseQuery => OnClose => OnDeactivate => OnHide => OnDestroy`
15+```text
16+OnCreate => OnShow => OnActivate => OnPaint => OnResize => OnPaint => ...
17+OnCloseQuery => OnClose => OnDeactivate => OnHide => OnDestroy
18+```
1719
1820### Form.OnCreate
1921
2022This is the equivalent to the class constructor in forms. Use this to initialize form-level variables etc. The form is not yet shown at this time.
2123
2224### Form.OnShow
2325
2426When the form is shown (e.g. when loading the form or setting its `.Visible` property to true), this event is fired - just before the form is visible. This allows you to modify the visual appearance of controls (e.g. disable certain controls) without flickering.
2527
2628### Form.OnActivate
2729
2830This occurs after Form.Show.\
2931At the OnActivate stage controls instantiated in OnCreate are present and correct, and should have the minimum needed properties set (such as Parent) plus anything else OnCreate specified.\
3032OnActivate signals that this form now has focus, so mouse/key events will start arriving.
3133
3234### Form.OnDeactivate
3335
3436Fired after the form loses focus.\
3537
3638> **Note:** Switching between different applications in the OS will not cause either Form.OnActivate or Form.OnDeactivate to fire. Thus Form.OnActivate/Form.OnDeactivate only track different forms changing focus within an application.\
3739> To track whether the Application *itself* receives or loses focus, use Application.OnActivate and Application.OnDeactivate.
3840
3941### Form.OnDestroy
4042
4143The equivalent to a class destructor in forms. Use this to clean up/free variables.\
4244When the main form of an application is destroyed, the application terminates.
4345
4446## General controls
4547
4648These events apply to various controls. Please use the Object Inspector to check if it is available for the control you're currently using.
4749
4850### OnEditingDone
4951
5052The OnEditingDone event for controls can act like the Validate event in other programming languages: it indicates the user is done changing the control and intends to keep that value. The program can now check the control content for correctness, show error messages, update database fields, etc.
5153
5254> **Note:** As soon as you click outside the control (even on a control that cannot receive focus) OnEditingDone is triggered.\
5355> (See TControl.MouseDown procedure, introduced in r11778).
5456
5557## Applicable Delphi information
5658
5759Because the implementation of events in Lazarus and Delphi is similar, a lot of Delphi-related documentation is applicable to Lazarus with minor modifications.
5860
5961[Delphi documentation](http://docwiki.embarcadero.com/RADStudio/en/Events) is often handy. You can search for the actual control you want to know more about to get a list of its events.
6062
6163If you want to know about the order in which events fire on a form, [this article from about.com](http://delphi.about.com/od/formsdialogs/a/delphiformlife.htm) can be useful.
6264
6365## See also
6466
6567- [[Lazarus Tutorial|Lazarus tutorial]]
6668- [[Drop files event]]
6769- [[Execute action after form is shown]]
6870
6971## External links
7072
7173- [Article about event order](http://delphi.about.com/od/formsdialogs/a/delphiformlife.htm)
7274- [Delphi documentation](http://docwiki.embarcadero.com/RADStudio/en/Events)
7375
7476---
7577*From the Free Pascal wiki, page "Event order", as it stood on 2019-12-02 after a change by Rfc1394, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
7678Category: Event-driven programming
7779Category: LCL
7880Category: Lazarus

Back to history