Recent

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

m4u_hoahoctro

  • Full Member
  • ***
  • Posts: 159
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: 2780
  • 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 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
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: 6918
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: 2968
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: 2
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: 1235
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 1.8.4/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3 (Home/Prof.), various DOSes.

J-G

  • Hero Member
  • *****
  • Posts: 523
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: 1333
    • 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: 7798
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.
Ad Brexinitum (can't help it)

Munair

  • Sr. Member
  • ****
  • Posts: 415
  • Keep it simple.
    • Ditrianum
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).
Lazarus 2.0.0; Manjaro Linux; Windows 10 x64;

Zoran

  • Hero Member
  • *****
  • Posts: 1333
    • 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: 13
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.