Recent

Author Topic: Application.OnDeactivate  (Read 1815 times)

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Application.OnDeactivate
« on: August 31, 2020, 12:03:18 am »
How do I use the Application.OnDeactivate event and it's opposite?

I've had a scratch around the Forum but have not found code I can understand or suitable.  %)

My object is to build an AutoSave feature that would save data from different components and memory.
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

jamie

  • Hero Member
  • *****
  • Posts: 7701
Re: Application.OnDeactivate
« Reply #1 on: August 31, 2020, 12:21:15 am »
Looks like you need to set it yourself during startup of your app..

You can do it from the MainForm.OnCreate cycle..

TO make it easier for you, implement the OnDeactivate event for the main form and during the OnCreateForm event manually assign the same event to the Application.OnDeactive event so you don't need to manually create one.

 While in the event, test for the SENDER to see who it is.., if its the Application object then proceed otherwise it would be the Form.OnDeactivate  sending it.

 Of course if this confuses  you , you could manually create a TNotifyEvent method and place it in the main form's class and then assign that event to the Application.OnDeactivate event etc..

The only true wisdom is knowing you know nothing

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Application.OnDeactivate
« Reply #2 on: August 31, 2020, 12:42:36 am »

TO make it easier for you, implement the OnDeactivate event for the main form and during the OnCreateForm event manually assign the same event to the Application.OnDeactive event so you don't need to manually create one.

 While in the event, test for the SENDER to see who it is.., if its the Application object then proceed otherwise it would be the Form.OnDeactivate  sending it.

 Of course if this confuses  you , you could manually create a TNotifyEvent method and place it in the main form's class and then assign that event to the Application.OnDeactivate event etc..

Do you have any examples? I am familair with FormCreate and FormCloseQuery, and FormClose (where I assume I will have to destroy the event).

Thanks,



Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

jamie

  • Hero Member
  • *****
  • Posts: 7701
Re: Application.OnDeactivate
« Reply #3 on: August 31, 2020, 12:50:53 am »
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  3.  
  4. type
  5.  
  6.   { TForm1 }
  7.  
  8.   TForm1 = class(TForm)
  9.     procedure FormCreate(Sender: TObject);
  10.   private
  11.  
  12.   public
  13.    Procedure AppDeActivate(Sender:Tobject);
  14.   end;
  15.  
  16. var
  17.   Form1: TForm1;
  18.  
  19. implementation
  20.  
  21. {$R *.lfm}
  22.  
  23. procedure TForm1.FormCreate(Sender: TObject);
  24. begin
  25.   Application.OnDeactivate := @Self.AppDeActivate;
  26. end;
  27.  
  28. Procedure TForm1.AppDeActivate(Sender:TObject);
  29. begin
  30.   Beep;
  31. end;
  32.  
  33. end.                            
  34.  

Do what you need to do in the event, but for now you can play with that and you will get a beep when it looses focus
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Application.OnDeactivate
« Reply #4 on: August 31, 2020, 01:13:06 am »
Or in case you do not wish to assign it manually, rather at design-time you can place an applicationproperties component on your form and assign the event, see https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tapplicationproperties.html
Today is tomorrow's yesterday.

jamie

  • Hero Member
  • *****
  • Posts: 7701
Re: Application.OnDeactivate
« Reply #5 on: August 31, 2020, 01:16:40 am »
Yeah that too, I forgot about that one...

Good one!
The only true wisdom is knowing you know nothing

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Application.OnDeactivate
« Reply #6 on: August 31, 2020, 03:23:32 am »
Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. begin
  4.   Application.OnDeactivate := @Self.AppDeActivate;
  5. end;
  6.  
  7.  

Do what you need to do in the event, but for now you can play with that and you will get a beep when it looses focus

Thanks Jamie. Worked instantly.

I then added an OnActivation with its own beep. Works good too.

I see good old Tron is offering an alternative. Will look into that also.

Many thanks

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

 

TinyPortal © 2005-2018