Recent

Author Topic: Detecting when second form is closed/hidden  (Read 2491 times)

GeneCode

  • New Member
  • *
  • Posts: 25
  • What is the cost of lies?
Detecting when second form is closed/hidden
« on: October 08, 2019, 03:25:00 am »
In my application, i have 2 forms:
1.MainForm
2.SettingsForm

A button A on MainForm opens the SettingsForm.
A button B on SettingsForm calls Close on itself (ie close form).

What I want to know is how to tell MainForm that a user has clicked button B?
Windows 10
Lazarus 1.8.4

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: Detecting when second form is closed/hidden
« Reply #1 on: October 08, 2019, 04:40:02 am »
Maybe what you need is ShowModal:
https://wiki.freepascal.org/Form_Tutorial#Difference_between_Show_and_ShowModal

What I want to know is how to tell MainForm that a user has clicked button B?

No, that is not good enough. Because users can close the Setting form without clicking button B. For example: Alt+F4 can be used for closing form on most OSes.
« Last Edit: October 08, 2019, 04:42:27 am by Handoko »

440bx

  • Hero Member
  • *****
  • Posts: 4014
Re: Detecting when second form is closed/hidden
« Reply #2 on: October 08, 2019, 07:47:37 am »
What I want to know is how to tell MainForm that a user has clicked button B?
Under Windows, when button B is clicked you could post a custom/"user defined" message to the MainForm window thus informing it that button B has been clicked.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Detecting when second form is closed/hidden
« Reply #3 on: October 08, 2019, 08:12:53 am »
Uh? Why not just use the "OnClose"-Event of Form2?
Inside the Event just call a public Procedure of Form1 (i do hope you're not closing Form1 when calling Form2).
I wouldn't be surprised if there's even a "direct" way to fire the Event from Form2 inside Form1
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Detecting when second form is closed/hidden
« Reply #4 on: October 08, 2019, 10:36:39 am »
I always use Assigned(SomeForm) to know if some form has been created (and if not, create it) and SomeForm.IsVisible or SomeForm.Visible to know whether it's showing or not. For example:

Code: Pascal  [Select][+][-]
  1. if not Assigned(SomeForm) then
  2.   Application.CreateForm(TSomeForm, SomeForm);
  3. if not SomeForm.Visible then
  4.   SomeForm.ShowOnTop;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Detecting when second form is closed/hidden
« Reply #5 on: October 08, 2019, 09:58:45 pm »
Uh? Why not just use the "OnClose"-Event of Form2?

Agreed.  And/or the OnShow and OnHide events, too.

I wouldn't be surprised if there's even a "direct" way to fire the Event from Form2 inside Form1

Sure there is - you can simply assign methods of Form1 as the actual event handlers for Form2's events.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Detecting when second form is closed/hidden
« Reply #6 on: October 09, 2019, 09:54:04 am »
I wouldn't be surprised if there's even a "direct" way to fire the Event from Form2 inside Form1

Sure there is - you can simply assign methods of Form1 as the actual event handlers for Form2's events.
Remy, got an example for that? I was fiddling around with that idea, but couldn't get it to work
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: Detecting when second form is closed/hidden
« Reply #7 on: October 09, 2019, 10:21:38 am »
Try the project in the attachment. I think it is important to note that this demo follows that principle that the MainForm "knows" the secondary forms, but each secondary form does not "know" the MainForm. Otherwise it is too easy to end up with circular references. Providing an event handler for the secondary form by the main form is the technique to accomplish that.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Detecting when second form is closed/hidden
« Reply #8 on: October 09, 2019, 11:59:37 am »
WP +1
Thx!
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

GeneCode

  • New Member
  • *
  • Posts: 25
  • What is the cost of lies?
Re: Detecting when second form is closed/hidden
« Reply #9 on: October 14, 2019, 04:24:17 am »
I actually ended up with using a global variable. On main form i have a timer repeated every 1 sec (sort of heartbeat), so on second form on closing, i put

Code: Pascal  [Select][+][-]
  1.     global_Reload := true;

Then on timer callback on main form, i have the following

Code: Pascal  [Select][+][-]
  1.   if (global_Reload) then begin
  2.     global_Reload := false;
  3.     LoadIPAddresses;
  4.   end;  

Bit hacky but works. I guess zvoni way is best to have public procedure on MainForm and call it from second form. Ill give it a try later. Thanks all. ;)

Windows 10
Lazarus 1.8.4

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Detecting when second form is closed/hidden
« Reply #10 on: October 14, 2019, 06:59:14 am »
I am using this construction:
Code: Pascal  [Select][+][-]
  1.   for maxid := 1 to Screen.FormCount - 1 do begin
  2.   // begin at 1 Because main form is always open.
  3.     if Screen.Forms[maxid].Visible then begin
  4.       Form_Message.MsgWindow(mVraag1, bOk,
  5.         'First close the active window' + sCrLf +
  6.         'before opening a new one !!', PrgNaam);
  7.       exit;
  8.     end;  // if
  9.   end;  // for
  10.  
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Detecting when second form is closed/hidden
« Reply #11 on: October 14, 2019, 07:14:30 am »
You can also use the observer/observed functionality, see https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.fponotifyobservers.html
This feature is a bit under-rated, but very powerful.
Have one form implement the observer and the other one IFPObserved, In the OnMinimize and OnClose event from the observed form, notify the observer form. Since TForm derives from Tpersistent, most of this functionality is free...
You can use ooCustom as observer notification and the data parameter to set minimize or close actions.

A basic implementation for Tstrings to get you started is here: http://free-pascal-general.1045716.n5.nabble.com/attachment/5711985/0/fpobserver_demo.pas
« Last Edit: October 14, 2019, 07:20:37 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

GeneCode

  • New Member
  • *
  • Posts: 25
  • What is the cost of lies?
Re: Detecting when second form is closed/hidden
« Reply #12 on: October 14, 2019, 07:56:39 am »
You can also use the observer/observed functionality, see https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.fponotifyobservers.html
This feature is a bit under-rated, but very powerful.
Have one form implement the observer and the other one IFPObserved, In the OnMinimize and OnClose event from the observed form, notify the observer form. Since TForm derives from Tpersistent, most of this functionality is free...
You can use ooCustom as observer notification and the data parameter to set minimize or close actions.

A basic implementation for Tstrings to get you started is here: http://free-pascal-general.1045716.n5.nabble.com/attachment/5711985/0/fpobserver_demo.pas

Oh wow. This is so familiar like in XCode development (NSNotificationCenter).
Thanks for sharing.... definitely will look into this.
Windows 10
Lazarus 1.8.4

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Detecting when second form is closed/hidden
« Reply #13 on: October 14, 2019, 08:20:42 am »
[Oh wow. This is so familiar like in XCode development (NSNotificationCenter).
Thanks for sharing.... definitely will look into this.
That is indeed also the observer pattern in action  :) 8-)
As I wrote: under rated but very powerful for such things. Also rather easy to implement in a few lines of code.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018