Recent

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

GeneCode

  • New Member
  • *
  • Posts: 16
  • 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: 3152
  • 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: 1123
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.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Zvoni

  • Full Member
  • ***
  • Posts: 236
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

lucamar

  • Hero Member
  • *****
  • Posts: 2012
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 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 664
    • 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

  • Full Member
  • ***
  • Posts: 236
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

wp

  • Hero Member
  • *****
  • Posts: 6225
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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Zvoni

  • Full Member
  • ***
  • Posts: 236
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

GeneCode

  • New Member
  • *
  • Posts: 16
  • What is the cost of lies?
Re: Detecting when second form is closed/hidden
« Reply #9 on: Today at 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: 688
  • ..... A day not Laughed is a day not Lived !!
    • Nursing With Humour
Re: Detecting when second form is closed/hidden
« Reply #10 on: Today at 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 2.0.2 / FPC 3.0.4
Lazarus Trunc / FPC 3.0.4
Mac OS X Mojave

Thaddy

  • Hero Member
  • *****
  • Posts: 8869
Re: Detecting when second form is closed/hidden
« Reply #11 on: Today at 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: Today at 07:20:37 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

GeneCode

  • New Member
  • *
  • Posts: 16
  • What is the cost of lies?
Re: Detecting when second form is closed/hidden
« Reply #12 on: Today at 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: 8869
Re: Detecting when second form is closed/hidden
« Reply #13 on: Today at 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.
Most people that want to use threading should learn to patch their jeans first: use a needle.