Recent

Author Topic: [solved] can't close app with 2 forms  (Read 1477 times)

speter

  • Sr. Member
  • ****
  • Posts: 419
[solved] can't close app with 2 forms
« on: July 19, 2025, 05:40:57 am »
G'Day Folks,

I have a project with 2 forms, I am using form2.show to display the second form, calling it in form1's formactivate method.

Form2.visible = false in the object inspector.

I have created a minimal project and attached it.

When the application runs (Windows 11; Lazarus 4.0), the second form gets focus and clicking on form1 seems to be ignored. Hitting the close window "X" in either form is also ignored. I am not sure what the issue is!?

Thanks in advance.
S.

« Last Edit: July 19, 2025, 06:24:07 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 18356
  • Here stood a man who saw the Elbe and jumped it.
Re: can't close app with 2 forms
« Reply #1 on: July 19, 2025, 05:54:34 am »
Of course that does not work:
Form2 show
Form1 Activates ,Form2 Show
Form1 Activates, etc.

OnActivate occurs multiple times, so form2.show also executes multiple times. OnCreate executes only once.
« Last Edit: July 19, 2025, 05:56:16 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

speter

  • Sr. Member
  • ****
  • Posts: 419
Re: can't close app with 2 forms
« Reply #2 on: July 19, 2025, 06:08:56 am »
OnActivate occurs multiple times, so form2.show also executes multiple times. OnCreate executes only once.
Thanks for the quick reply.

I wasn't using OnCreate because I wasn't sure the second form would have been created.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

speter

  • Sr. Member
  • ****
  • Posts: 419
Re: can't close app with 2 forms
« Reply #3 on: July 19, 2025, 06:23:33 am »
I found that if I had form2.show in form1's OnCreate, there was an error (because form2=nil).

The solution was to add a boolean to make sure form2.show only fires once:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   done := false;
  4. end;
  5.  
  6. procedure TForm1.FormActivate(Sender: TObject);
  7. begin
  8.   if done then exit;
  9.   form2.show;
  10.   done := true;
  11. end;

This works as expected; either window's close "X" will work, closing the form or the application.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 18356
  • Here stood a man who saw the Elbe and jumped it.
Re: [solved] can't close app with 2 forms
« Reply #4 on: July 19, 2025, 11:54:53 am »
Better connect the event to Application.OnActivate and disconnect it from the form.
Then you don't need the boolean.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

CharlyTango

  • Full Member
  • ***
  • Posts: 169
Re: [solved] can't close app with 2 forms
« Reply #5 on: July 19, 2025, 04:29:26 pm »
What i don't understand is that several programmers let lazarus create all the forms of a project. Thus all Forms are created at startup.

This strategy is quite alien to me, because I want to determine the time of creation and destruction of objects myself.
I try to avoid such situations in which a form is called without user action.
The only form that is created and displayed by Lazarus in my programmes is the main form -- all other forms are created at runtime
Lazarus stable, Win32/64

Thaddy

  • Hero Member
  • *****
  • Posts: 18356
  • Here stood a man who saw the Elbe and jumped it.
Re: [solved] can't close app with 2 forms
« Reply #6 on: July 19, 2025, 05:26:40 pm »
What i don't understand is that several programmers let lazarus create all the forms of a project. Thus all Forms are created at startup.
That's the curse of Delphi and the ease of which VB programmers got access to a real language and subsequently started to abuse the possibilities.
Now it is deeply corroded into the system and users can no longer unbolt it....Because they do not know better.
You are absolutely right of course. Then again, I know commercial software with Button1 and Form1....Discovered by binary grep.
IMO that is even worse...
« Last Edit: July 19, 2025, 05:31:09 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018