Lazarus

Programming => General => Topic started by: m4u_hoahoctro on December 29, 2012, 01:47:13 pm

Title: How to create a button exit for app
Post by: m4u_hoahoctro on December 29, 2012, 01:47:13 pm
How to create a button with function: When press it, program will close ( button for form)
thanks
Title: Re: How to create a button exit for app
Post by: Blaazen on December 29, 2012, 01:54:11 pm
If it is a main form then simply put there a button and its OnClick method:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;
should be enough.
Title: Re: How to create a button exit for app
Post by: marcov on December 29, 2012, 03:46:45 pm
Won't that only close that form?

Application.terminate;

seems to be safer
Title: Re: How to create a button exit for app
Post by: howardpc on December 29, 2012, 03:55:44 pm
Closing the main form terminates the Application (as Blaazen stated). Of course closing any secondary form merely closes that window without affecting the rest of the process.
Title: Re: How to create a button exit for app
Post by: Redenegue on April 14, 2019, 09:21:12 pm
I have found that neither of these will work on a form that is minimized and borderless. Once minimized, you need to restore the program with the Windows icon. Then using a main menu exit either close or terminate the program. The program minimizes, it does not close. I have just started looking for a solution. This was my first stop.
Title: Re: How to create a button exit for app
Post by: lucamar on April 14, 2019, 10:50:38 pm
I've just tested and it works here (Laz 1.8.4/Linux 32 bit).
The only times a Form.Close doesn't also close the application are
These last two, of course, don't close the form either ...
Title: Re: How to create a button exit for app
Post by: J-G on April 15, 2019, 12:41:20 pm
I have found that neither of these will work on a form that is minimized and borderless. Once minimized, you need to restore the program with the Windows icon. Then using a main menu exit either close or terminate the program. The program minimizes, it does not close. I have just started looking for a solution. This was my first stop.

????   Surely, if the program is minimised there are no buttons visible !
Title: Re: How to create a button exit for app
Post by: Zoran on April 15, 2019, 01:59:01 pm
I have found that neither of these will work on a form that is minimized and borderless.

I understand you are able to click that button on the minimized form when you claim it does not work. ::)
Well, not that I can test it, but I would expect it to work. Of course, you still have to click on that button.
Title: Re: How to create a button exit for app
Post by: Thaddy on April 15, 2019, 02:10:27 pm
If it is a main form then simply put there a button and its OnClick method:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;
should be enough.
No. Closes the form.
Application terminate. As Marco pointed out.
Title: Re: How to create a button exit for app
Post by: munair on April 15, 2019, 03:29:24 pm
If it's the main form, Close() terminates the application (my experience).
Title: Re: How to create a button exit for app
Post by: Zoran on April 15, 2019, 03:34:39 pm
If it's the main form, Close() terminates the application (my experience).

Everyone's experience, since always. And documented. (https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tcustomform.close.html)

Thaddy, why don't you try it before posting?

Closing the main form terminates the application.
So Close should be enough after all. Unless, as Lucamar pointed out (https://forum.lazarus.freepascal.org/index.php/topic,19376.msg317594.html#msg317594), OnClose or OnCloseQuery do not intercept it and change this behaviour.
Title: Re: How to create a button exit for app
Post by: Raj Gupta on April 15, 2019, 06:49:54 pm
If it is a main form then simply put there a button and its OnClick method:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;
should be enough.


Its work for me to.
This is the correct method.
I have used it.
Title: Re: How to create a button exit for app
Post by: trev on February 28, 2021, 11:36:28 am
Yes, I know, this is an ancient thread but, while researching a similar issue, I discovered Redenegue was correct despite some poking fun at him. He mentioned Windows and my example is macOS when the one and only Form window is not the active one (eg it is occluded by another or it is minimised).

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Forms, StdCtrls, CocoaAll;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     procedure Button1Click(Sender: TObject);
  13.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  14.   end;
  15.  
  16. var
  17.   Form1: TForm1;
  18.  
  19. implementation
  20.  
  21. {$R *.lfm}
  22.  
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. begin
  25.   NSLog(NSStr('closing'));
  26.   //System.Exit;            // works
  27.   Application.Terminate;    // works
  28.   //Form1.Close; // results in abnormal program termination eventually after bouncing
  29.                  // back and forth between FormClose and Button1Click many times
  30. end;
  31.  
  32. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  33. begin
  34.   NSLog(NSStr('leaving...'));
  35.   Application.BringToFront; // needed
  36.   Button1.Click;
  37. end;
  38.  
  39. end.

And to explain how to exit while minimised or occluded, one right clicks on the macOS dock application icon and chooses the Quit option, similar to Windows where one would right click on the item in the task bar and choose Close.
Title: Re: How to create a button exit for app
Post by: lucamar on February 28, 2021, 12:43:03 pm
The button click should just call Close (for the form) which, if it's the main form, will close the application. That will in turn fire the OnClose event: if you re-Click the button there you are caught in an infinite loop, so don't do that.

When the application is closed externally (e.g. by the methods you cite), the OnClose event will be fired, just as if you had called Close, so there is no need (or shouldn't be) to call the button's OnClick handler.
Title: Re: How to create a button exit for app
Post by: trev on February 28, 2021, 01:44:09 pm
When the application is closed externally (e.g. by the methods you cite), the OnClose event will be fired, just as if you had called Close, so there is no need (or shouldn't be) to call the button's OnClick handler.

In fact it does terminate if I wait long enough (long enough being about 10 seconds). Not really the user experience one would want though and I'd missed it because I had not been waiting that long.
Title: Re: How to create a button exit for app
Post by: lucamar on February 28, 2021, 05:29:08 pm
In fact it does terminate if I wait long enough (long enough being about 10 seconds). Not really the user experience one would want though and I'd missed it because I had not been waiting that long.

That's indeed quite a lot of time nowadays, more so if when you exit normally (by calling the mainform's Close or through the "normal" means) it doesn't happen. I know next to nothing about Macs so I can't help much more, sorry.
Title: Re: How to create a button exit for app
Post by: dseligo on March 01, 2021, 10:10:50 am
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   NSLog(NSStr('leaving...'));
  4.   Application.BringToFront; // needed
  5. //  Button1.Click;
  6. end;
You shouldn't have Button1.Click in FormClose if you have Form1.Close in Button1Click. FormClose is called after Form1.Close.
TinyPortal © 2005-2018