Sign in Join
Wiki copytemporary

Execute action after form is shown: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Execute action after form is shown

OldNewLine
1+Lazarus doesn't support something like form OnShown/OnShowAfter event where custom code can be executed after form was shown. This is useful mainly to do run custom code after main form is shown. It would serve as kind of one time OnApplicationStart event. In would be useful for example for loading of previous opened project/game file just after application start which can take longer time and main form should be already visible at that moment.
2+
3+There are multiple ways how to solve this problem.
4+
5+## Methods
6+
7+### OnActivate event
8+
9+Form OnActivate event is fired every time if form gets focus. Also this event is fired after OnShow event. Then it is possible to use this event to executed code only once after OnShow event. Boolean variable is needed so after form shown code will be executed only once.
10+
11+```delphi
12+type
13+ TFormMain = class
14+ ...
15+ private
16+ FormActivated: Boolean;
17+ ...
18+ end;
19+
20+...
21+
22+procedure TFormMain.FormActivated(Sender: TObject);
23+begin
24+ if not FormActivated then begin
25+ FormActivated := True;
26+ // After show code
27+ end;
28+end;
29+```
30+
31+### Send custom message
32+
33+It is possible to define custom message and send it to application message queue so it will be handled as soon as possible.
34+
35+```delphi
36+uses
37+ ..., LMessages, LCLIntf;
38+
39+const
40+ LM_STARTUP = LM_USER;
41+
42+procedure TFormMain.FormShow(Sender: TObject);
43+begin
44+ // On on show code
45+ SendMessage(Handle, LM_STARTUP, 0, 0);
46+end;
47+
48+procedure TFormMain.LMStartup(var Msg: TLMessage);
49+begin
50+ // After show code
51+end;
52+```
53+
54+### QueueAsyncCall
55+
56+There is a way how to execute asynchronous operations in Lazarus using [[Asynchronous Calls]]. This is useful mainly for parallel processing but it can be used also for execution of form after show handler.
57+
58+```delphi
59+{$mode delphi}
60+
61+procedure TFormMain.FormShow(Sender: TObject);
62+begin
63+ // On on show code
64+ Application.QueueAsyncCall(AfterShow, 0);
65+end;
66+
67+procedure TFormMain.AfterShow(Ptr: IntPtr);
68+begin
69+ // After show code
70+end;
71+```
72+
73+### TTimer
74+
75+You can use TTimer instance for delayed execution of startup code. Create instance of TTimer component and set its property Enabled to False and Interval to 1. Then from the end of FormShow handler enable timer manually. It will be executed as soon as possible. You need to disable timer at the start of timer handler.
76+
77+```delphi
78+procedure TFormMain.FormShow(Sender: TObject);QueueAsyncCall
79+begin
80+ // On on show code
81+ Timer1.Enabled := True;
82+end;
83+
84+procedure TForm1.Timer1Timer(Sender: TObject);
85+begin
86+ Timer1.Enabled := False;
87+ // After show code
88+end;
89+```
90+
91+## See also
92+
93+- [[Event order]]
94+
95+---
96+*From the Free Pascal wiki, page "Execute action after form is shown", as it stood on 2018-09-09 after a change by Chronos, 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).*
97+Category: Lazarus

Back to history