Recent

Author Topic: Correct way to close a form  (Read 1483 times)

lander

  • New Member
  • *
  • Posts: 31
Correct way to close a form
« on: October 05, 2021, 08:51:43 am »
Hi, I have searched on google already and there are various ways to close a form but I'm confused.
Which is the correct way to open Form2 and close Form1 freeing the space from memory?
« Last Edit: October 05, 2021, 09:00:22 am by lander »

Handoko

  • Hero Member
  • *****
  • Posts: 4384
  • My goal: build my own game engine using Lazarus
Re: Correct way to close a form
« Reply #1 on: October 05, 2021, 09:17:02 am »
Hello lander,
Welcome to the forum.

If Form1 is the mainform, you should not close it. Closing mainform will terminate the program. Instead, you can hide the mainform before showing the Form2.

lander

  • New Member
  • *
  • Posts: 31
Re: Correct way to close a form
« Reply #2 on: October 05, 2021, 09:24:13 am »
Thank you for your fast reply.
And to correctly close the second form as mentioned in first post?
Form2.Destroy, .Free or .Close?

Instead for the first form I can just do .Hide?

Zvoni

  • Hero Member
  • *****
  • Posts: 808
Re: Correct way to close a form
« Reply #3 on: October 05, 2021, 11:50:46 am »
Never ever call the "Destroy"-Method (of any Class) directly.

And just use "sane" thinking:
You exit your car
you CLOSE the door
you lock your car, FREEing your mind so you don't have to fret about it if someone is going to steal it
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

Handoko

  • Hero Member
  • *****
  • Posts: 4384
  • My goal: build my own game engine using Lazarus
Re: Correct way to close a form
« Reply #4 on: October 05, 2021, 11:51:54 am »
Below is a demo doing the thing OP said. Here you can learn:
- How to create a splash screen
- Loading image from application resources

Code: Pascal  [Select][+][-]
  1. unit uniForm1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, ExtCtrls, StdCtrls, uniForm2;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Label1: TLabel;
  16.     Timer1: TTimer;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure FormPaint(Sender: TObject);
  20.     procedure Timer1Timer(Sender: TObject);
  21.   private
  22.     FDelay: Integer;
  23.     FBackground: TBitmap;
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. var
  37.   Stream: TResourceStream;
  38.   Image:  TImage;
  39. begin
  40.   // Make it center and borderless
  41.   BorderStyle := bsNone;
  42.   Position    := poScreenCenter;
  43.   // Get the background image from application resource
  44.   FBackground := TBitmap.Create;
  45.   Stream      := TResourceStream.Create(HInstance, 'BACKGROUND', RT_RCDATA);
  46.   Image       := TImage.Create(nil);
  47.   Image.Picture.LoadFromStream(Stream);
  48.   FBackground.Assign(Image.Picture.Bitmap);
  49.   Image.Free;
  50.   Stream.Free;
  51.   // Prepare the timer
  52.   FDelay         := 5;
  53.   Label1.Caption := FDelay.ToString;
  54. end;
  55.  
  56. procedure TForm1.FormDestroy(Sender: TObject);
  57. begin
  58.   FBackground.Free;
  59. end;
  60.  
  61. procedure TForm1.FormPaint(Sender: TObject);
  62. begin
  63.   Canvas.Draw(0, 0, FBackground);
  64. end;
  65.  
  66. procedure TForm1.Timer1Timer(Sender: TObject);
  67. begin
  68.   // Show the countdown
  69.   Dec(FDelay);
  70.   Label1.Caption := FDelay.ToString;
  71.   if FDelay > 0 then Exit;
  72.   // Hide the form
  73.   Timer1.Enabled := False;
  74.   Visible        := False;
  75.   Form2.Show;
  76. end;
  77.  
  78. end.

- Form1 now becomes a splash form
- You should treat Form2 as a mainform
- But closing Form2 won't automatically terminate the program

Because system does not know Form2 should be treated as mainform, closing it will not automatically terminate the program. You need to manually terminate it. Try running the demo, uncheck the checkbox and close the form to see what will happen.

There many different ways to create a splash screen, you can search the forum learn how others do it.
« Last Edit: October 05, 2021, 12:02:25 pm by Handoko »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9706
  • FPC developer.
Re: Correct way to close a form
« Reply #5 on: October 05, 2021, 04:36:39 pm »
I'm no GUI hero, but afaik it depends how your form was created. Autocreated forms are registered with application and released with them. (application.createform).

Manual will have NIL as parent, and can just be closed and free.

Don't forget to disable any events coming into the form before doing that.

lander

  • New Member
  • *
  • Posts: 31
Re: Correct way to close a form
« Reply #6 on: October 06, 2021, 08:14:31 pm »
Thanks for your help.

 

TinyPortal © 2005-2018