Recent

Author Topic: OnCreate and OnActivate  (Read 12409 times)

KMagic

  • Full Member
  • ***
  • Posts: 100
OnCreate and OnActivate
« on: June 05, 2014, 09:35:54 am »
Could anyone tell me the difference between OnCreate and OnActivate event of a form?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: OnCreate and OnActivate
« Reply #1 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.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: OnCreate and OnActivate
« Reply #2 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).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: OnCreate and OnActivate
« Reply #3 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.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: OnCreate and OnActivate
« Reply #4 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...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: OnCreate and OnActivate
« Reply #5 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...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: OnCreate and OnActivate
« Reply #6 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.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: OnCreate and OnActivate
« Reply #7 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.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: OnCreate and OnActivate
« Reply #8 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...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: OnCreate and OnActivate
« Reply #9 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
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: OnCreate and OnActivate
« Reply #10 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!  :)
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: OnCreate and OnActivate
« Reply #11 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.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: OnCreate and OnActivate
« Reply #12 on: June 05, 2014, 08:29:03 pm »
Thanks a lot - appreciate it - keep on stealing ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: OnCreate and OnActivate
« Reply #13 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.

jipété

  • Full Member
  • ***
  • Posts: 113
Re: OnCreate and OnActivate
« Reply #14 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,
« Last Edit: September 13, 2021, 11:53:41 am by jipété »

 

TinyPortal © 2005-2018