Recent

Author Topic: How to create a button exit for app  (Read 21439 times)

m4u_hoahoctro

  • Full Member
  • ***
  • Posts: 160
How to create a button exit for app
« 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

Blaazen

  • Hero Member
  • *****
  • Posts: 3034
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to create a button exit for app
« Reply #1 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.
Lazarus 2.1.0 r64546 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9286
  • FPC developer.
Re: How to create a button exit for app
« Reply #2 on: December 29, 2012, 03:46:45 pm »
Won't that only close that form?

Application.terminate;

seems to be safer

howardpc

  • Hero Member
  • *****
  • Posts: 3703
Re: How to create a button exit for app
« Reply #3 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.

Redenegue

  • Newbie
  • Posts: 3
Re: How to create a button exit for app
« Reply #4 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.

lucamar

  • Hero Member
  • *****
  • Posts: 3916
Re: How to create a button exit for app
« Reply #5 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
  • when it's not the Application.MainForm;
  • when one sets a handler for the form's OnClose event and changes CloseAction; or
  • when one sets a handler for the form's OnCloseQuery event and sets CanClose to False.
These last two, of course, don't close the form either ...
« Last Edit: April 14, 2019, 10:54:50 pm by lucamar »
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.

J-G

  • Hero Member
  • *****
  • Posts: 559
Re: How to create a button exit for app
« Reply #6 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 !
FPC 3.0.0  Lazarus 1.6  Win 7 Ult 64

Zoran

  • Hero Member
  • *****
  • Posts: 1667
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How to create a button exit for app
« Reply #7 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
Re: How to create a button exit for app
« Reply #8 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.

munair

  • Hero Member
  • *****
  • Posts: 698
  • keep it simple
    • SharpBASIC
Re: How to create a button exit for app
« Reply #9 on: April 15, 2019, 03:29:24 pm »
If it's the main form, Close() terminates the application (my experience).

Zoran

  • Hero Member
  • *****
  • Posts: 1667
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How to create a button exit for app
« Reply #10 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.

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, OnClose or OnCloseQuery do not intercept it and change this behaviour.

Raj Gupta

  • New Member
  • *
  • Posts: 34
Re: How to create a button exit for app
« Reply #11 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.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1443
  • Former Delphi 1-7, 10.2 user
Re: How to create a button exit for app
« Reply #12 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.
« Last Edit: February 28, 2021, 11:54:37 am by trev »
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r65070 FPC 3.3.1 r49223 macOS 11.2.3 aarch64 Xcode 12.4
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

lucamar

  • Hero Member
  • *****
  • Posts: 3916
Re: How to create a button exit for app
« Reply #13 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.
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.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1443
  • Former Delphi 1-7, 10.2 user
Re: How to create a button exit for app
« Reply #14 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.
« Last Edit: February 28, 2021, 01:56:21 pm by trev »
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r65070 FPC 3.3.1 r49223 macOS 11.2.3 aarch64 Xcode 12.4
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

 

TinyPortal © 2005-2018