Lazarus

Programming => General => Topic started by: KMagic on June 05, 2014, 09:35:54 am

Title: OnCreate and OnActivate
Post by: KMagic on June 05, 2014, 09:35:54 am
Could anyone tell me the difference between OnCreate and OnActivate event of a form?
Title: Re: OnCreate and OnActivate
Post by: BigChimp on June 05, 2014, 09:37:20 am
http://wiki.lazarus.freepascal.org/Event_order#Forms

;)

I think OnActivate takes place after the controls are set up etc and ready to be displayed visually; don't know for sure; you could have a look at the equivalent Delphi documentation (and e.g. about.com).. see the links at the bottom of that wiki page.
Title: Re: OnCreate and OnActivate
Post by: Blaazen on June 05, 2014, 09:44:39 am
OnActivate is triggered when you change focus of Forms of you application. If your app. has only one Form then it is triggered only one (after OnCreate).
Title: Re: OnCreate and OnActivate
Post by: howardpc on June 05, 2014, 09:45:16 am
OnActivate is fired after OnCreate.
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.
OnActivate signals that this form now has focus, so mouse/key events will start arriving.
Title: Re: OnCreate and OnActivate
Post by: BigChimp on June 05, 2014, 09:56:35 am
BTW, if anybody wants to update the wiki - that would certainly help others looking for the same info...
Title: Re: OnCreate and OnActivate
Post by: Mike.Cornflake on June 05, 2014, 10:12:16 am
OnActivate is triggered when you change focus of Forms of you application. If your app. has only one Form then it is triggered only one (after OnCreate).
I'm not in front of code at the moment, so can't confirm for Lazarus, but I'm sure this isn't the case.  Sure, if your application retains focus all the time, then OnActivate will only fire once.  However, if you switch focus between different running applications, then when you switched back to your application, the OnActivate would fire again for that form.

Quote
BTW, if anybody wants to update the wiki - that would certainly help others looking for the same info...
I'll run some tests on this tonight and do that.  There's another minor change to the wiki I want to do tonight, so while I'm there anyway...
Title: Re: OnCreate and OnActivate
Post by: Blaazen on June 05, 2014, 10:24:39 am
Quote
However, if you switch focus between different running applications, then when you switched back to your application, the OnActivate would fire again for that form.
Not true here. I tried with one-form-app, switching between Laz. and this app. and also switching to other virtual desktop and  back. OnActivate is triggered just once. I tried also with Delphi7 (uder Wine) and it is the same.
Title: Re: OnCreate and OnActivate
Post by: Zoran on June 05, 2014, 10:57:39 am
However, if you switch focus between different running applications, then when you switched back to your application, the OnActivate would fire again for that form.

No, I have just tested, TForm.OnActivate is triggered when focus is changed among forms within the application.
When the application receives focus, then TApplication.OnActivate is triggered.
Title: Re: OnCreate and OnActivate
Post by: Mike.Cornflake on June 05, 2014, 12:42:09 pm
Not true here. I tried with one-form-app, switching between Laz. and this app. and also switching to other virtual desktop and  back. OnActivate is triggered just once. I tried also with Delphi7 (uder Wine) and it is the same.
No, I have just tested, TForm.OnActivate is triggered when focus is changed among forms within the application.
When the application receives focus, then TApplication.OnActivate is triggered.

Happy to stand corrected :-)   Many thanks for the info...
Title: Re: OnCreate and OnActivate
Post by: Mike.Cornflake on June 05, 2014, 07:03:37 pm
Minor changes to the wiki made. 
Changed wiki to say OnActivate happens after OnShow, not OnCreate
Added OnDeactivate
Added note on difference between Form.OnActivate/OnDeactivate and Application.OnActivate/OnDeactivate.
Thought about adding a comment about single form apps, but decided what's there actually covers all examples.
http://wiki.lazarus.freepascal.org/Event_order#Forms
Title: Re: OnCreate and OnActivate
Post by: jwdietrich on June 05, 2014, 07:17:02 pm
Minor changes to the wiki made. 
Changed wiki to say OnActivate happens after OnShow, not OnCreate
Added OnDeactivate
Added note on difference between Form.OnActivate/OnDeactivate and Application.OnActivate/OnDeactivate.
Thought about adding a comment about single form apps, but decided what's there actually covers all examples.
http://wiki.lazarus.freepascal.org/Event_order#Forms

Thanks!  :)
Title: Re: OnCreate and OnActivate
Post by: Mike.Cornflake on June 05, 2014, 08:07:39 pm
Quote
Thought about adding a comment about single form apps, but decided what's there actually covers all examples.
Then changed my mind, but put mentions in OnShow and OnDestory :-)  Also, I finally noted the comment from @Howardpc below, liked it, stole it :-)
Quote
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.
OnActivate signals that this form now has focus, so mouse/key events will start arriving.
Title: Re: OnCreate and OnActivate
Post by: BigChimp on June 05, 2014, 08:29:03 pm
Thanks a lot - appreciate it - keep on stealing ;)
Title: Re: OnCreate and OnActivate
Post by: User137 on June 05, 2014, 10:24:54 pm
Basically it's possible for onActivate (when form is activated) to be called multiple times while program is running. onCreate however only happens once per form.
Title: Re: OnCreate and OnActivate
Post by: jipété on September 13, 2021, 11:52:04 am
Hi !

wiki is showing events in wrong order, at least within Linux Debian Wheezy gtk2 :
I read
Quote
OnCreate => OnShow => OnActivate => OnPaint => OnResize => OnPaint => ...
But I get
Quote
OnCreate => OnResize => OnShow => OnPaint => (OnActivate =>) OnPaint => ...
Removing the OnPaint event allows for OnActivate to be fully displayed.

Test code :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);// 1
  2. begin
  3.   showmessage('create');
  4.   caption := 'create'; // not seen coz' form not displayed
  5.   application.ProcessMessages;
  6. end;
  7.  
  8. procedure TForm1.FormResize(Sender: TObject);// 2
  9. begin
  10.   showmessage('resize');
  11.   caption := 'resize'; // not seen coz' form not displayed
  12.   application.ProcessMessages;
  13. end;
  14.  
  15. procedure TForm1.FormShow(Sender: TObject);// 3
  16. begin
  17.   showmessage('show');
  18.   caption := 'show'; // not seen coz' form not displayed
  19.   application.ProcessMessages;
  20. end;
  21.  
  22. procedure TForm1.FormPaint(Sender: TObject); // 4 maybe but unable to be sure : empty form
  23. begin
  24.   //showmessage('paint'); // not possible : deadlock always repainting after closing the dialog
  25.   caption := 'paint';
  26.   application.ProcessMessages;
  27. end;
  28.  
  29. procedure TForm1.FormActivate(Sender: TObject);// 4 if no paint event
  30. begin
  31.   showmessage('activate');
  32.   caption := 'activate'; // it's 'paint' which is visible, lol, coz' it repaints form at the end...
  33. // if paint event commented out, it's ok here.
  34.   application.ProcessMessages;
  35. end;
Regards,
Title: Re: OnCreate and OnActivate
Post by: howardpc on September 13, 2021, 05:41:34 pm
A typical linux has GUI form events in this order:
Code: Pascal  [Select][+][-]
  1. TForm1.FormCreate
  2. TForm1.FormShow
  3. TForm1.FormPaint
  4. TForm1.FormResize
  5. TForm1.FormActivate
  6. TForm1.FormPaint
  7. TForm1.FormClose
  8. TForm1.FormDestroy
However, the order of paint (and other) events may be widgetset dependent, as well as dependent on event-manipulating code your app may add.
Title: Re: OnCreate and OnActivate
Post by: wp on September 13, 2021, 07:40:17 pm
Test code :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);// 1
  2. begin
  3.   showmessage('create');
  4.   caption := 'create'; // not seen coz' form not displayed
  5.   application.ProcessMessages;
  6. end;
  7. [...]
I would not use ShowMessage in an event logging test program because in order to show the message the message dialog will get focus and may change the normal flow of events in Form1. It is much better to add a Memo to Form1 and call "Memo.Lines.Add(---message---)" in the event handlers rather than ShowMessage.
Title: Re: OnCreate and OnActivate
Post by: Mr.Madguy on September 14, 2021, 04:58:41 pm
It's much easier to understand this events on Windows, because they're just equivalents of corresponding window messages.
WM_CREATE (https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-create)
WM_ACTIVATE (https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-activate)
Title: Re: OnCreate and OnActivate
Post by: jipété on September 17, 2021, 12:06:52 am
It is much better to add a Memo to Form1 and call "Memo.Lines.Add(---message---)" in the event handlers rather than ShowMessage.
Thx for that idea, it's ok.
I got that :
Code: Pascal  [Select][+][-]
  1. create
  2. resize
  3. show
  4. activate
  5. paint
  6. paint
  7. ...
Regards,
--
jp
Title: Re: OnCreate and OnActivate
Post by: dbannon on September 17, 2021, 02:53:16 am
As jipété appears to be a Linux user, a far simpler approach is possible, put writeln('<Event Name>') in each event handler, that way, they appear even before the form is shown and in the actual order. See the printout in View->DebugWindows->ConsoleIn/Out.

You can do the same in Windows using debugln() but need to capture that output to a file (or tell the app it does really have a console...).

Davo
TinyPortal © 2005-2018