Event order: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | 1 | ## Overview | |
| 18 | 2 | ||
| 19 | - | ||
| 3 | + | Lazarus 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). | |
| 20 | 4 | ||
| 21 | - | ||
| 5 | + | There 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. | |
| 22 | 6 | ||
| 23 | 7 | ## Lazarus documentation | |
| 24 | 8 | ||
| 25 | - | ||
| 9 | + | The [[Lazarus Tutorial|Lazarus tutorial]] has some information on what events do what. | |
| 26 | 10 | ||
| 27 | 11 | ## [[TForm|Forms]] | |
| 28 | 12 | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 13 | + | Typical event order for [[TForm|forms]] is: | |
| 34 | 14 | ||
| 15 | + | `OnCreate => OnShow => OnActivate => OnPaint => OnResize => OnPaint => ... `\ | |
| 16 | + | `OnCloseQuery => OnClose => OnDeactivate => OnHide => OnDestroy` | |
| 17 | + | ||
| 35 | 18 | ### Form.OnCreate | |
| 36 | 19 | ||
| 37 | - | ||
| 20 | + | This 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. | |
| 38 | 21 | ||
| 39 | 22 | ### Form.OnShow | |
| 40 | 23 | ||
| 41 | - | ||
| 24 | + | When 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. | |
| 42 | 25 | ||
| 43 | 26 | ### Form.OnActivate | |
| 44 | 27 | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 28 | + | This occurs after Form.Show.\ | |
| 29 | + | At 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.\ | |
| 30 | + | OnActivate signals that this form now has focus, so mouse/key events will start arriving. | |
| 48 | 31 | ||
| 49 | 32 | ### Form.OnDeactivate | |
| 50 | 33 | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 34 | + | Fired after the form loses focus.\ | |
| 54 | 35 | ||
| 55 | - | ||
| 36 | + | > **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.\ | |
| 37 | + | > To track whether the Application *itself* receives or loses focus, use Application.OnActivate and Application.OnDeactivate. | |
| 56 | 38 | ||
| 57 | 39 | ### Form.OnDestroy | |
| 58 | 40 | ||
| 59 | - | ||
| 60 | - | ||
| 41 | + | The equivalent to a class destructor in forms. Use this to clean up/free variables.\ | |
| 42 | + | When the main form of an application is destroyed, the application terminates. | |
| 61 | 43 | ||
| 62 | 44 | ## General controls | |
| 63 | 45 | ||
| 64 | - | ||
| 46 | + | These events apply to various controls. Please use the Object Inspector to check if it is available for the control you're currently using. | |
| 65 | 47 | ||
| 66 | 48 | ### OnEditingDone | |
| 67 | 49 | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 50 | + | The 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. | |
| 71 | 51 | ||
| 72 | - | ||
| 52 | + | > **Note:** As soon as you click outside the control (even on a control that cannot receive focus) OnEditingDone is triggered.\ | |
| 53 | + | > (See TControl.MouseDown procedure, introduced in r11778). | |
| 73 | 54 | ||
| 74 | 55 | ## Applicable Delphi information | |
| 75 | 56 | ||
| 76 | - | ||
| 57 | + | Because the implementation of events in Lazarus and Delphi is similar, a lot of Delphi-related documentation is applicable to Lazarus with minor modifications. | |
| 77 | 58 | ||
| 78 | - | ||
| 59 | + | [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. | |
| 79 | 60 | ||
| 80 | - | ||
| 61 | + | If 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. | |
| 81 | 62 | ||
| 82 | 63 | ## See also | |
| 83 | 64 | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 65 | + | - [[Lazarus Tutorial|Lazarus tutorial]] | |
| 66 | + | - [[Drop files event]] | |
| 67 | + | - [[Execute action after form is shown]] | |
| 87 | 68 | ||
| 88 | 69 | ## External links | |
| 89 | 70 | ||
| 90 | - | ||
| 91 | - | ||
| 71 | + | - [Article about event order](http://delphi.about.com/od/formsdialogs/a/delphiformlife.htm) | |
| 72 | + | - [Delphi documentation](http://docwiki.embarcadero.com/RADStudio/en/Events) | |
| 92 | 73 | ||
| 93 | 74 | --- | |
| 94 | - | ||
| 75 | + | *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).* | |
| 76 | + | Category: Event-driven programming | |
| 77 | + | Category: LCL | |
| 78 | + | Category: Lazarus |
