Recent

Author Topic: Choose whether to close the program  (Read 9038 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2257
    • Lazarus and Free Pascal italian community
Choose whether to close the program
« on: July 29, 2010, 01:33:11 pm »
Iotated that when I close the window, I asked if I really want to close the program! How do I do if I say no to not close the program?

Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1931
Re: Choose whether to close the program
« Reply #1 on: July 29, 2010, 02:07:35 pm »
TForm has event OnCloseQuery.
You can set parameter "CanClose" to false.

xinyiman

  • Hero Member
  • *****
  • Posts: 2257
    • Lazarus and Free Pascal italian community
Re: Choose whether to close the program
« Reply #2 on: July 29, 2010, 02:24:04 pm »
sample example
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Choose whether to close the program
« Reply #3 on: July 29, 2010, 02:39:21 pm »
For completeness:
(add LclType unit)

Code: [Select]
if Application.MessageBox('Are you sure you want to quit?', 'Question', MB_YesNo + MB_IconQuestion) = IdYes then Application.Terminate else CanClose := False;
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

xinyiman

  • Hero Member
  • *****
  • Posts: 2257
    • Lazarus and Free Pascal italian community
Re: Choose whether to close the program
« Reply #4 on: July 29, 2010, 02:45:32 pm »
Error example

Hint: Start of reading config file C:\lazarus\fpc\2.4.0\bin\i386-win32\fpc.cfg
Hint: End of reading config file C:\lazarus\fpc\2.4.0\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 2.4.0 [2010/07/19] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Win32 for i386
Compiling project1.lpr
Compiling unit1.pas
unit1.pas(29,24) Hint: Parameter "Sender" not used
unit1.pas(28,27) Hint: Parameter "Sender" not used
unit1.pas(26,30) Hint: Parameter "Sender" not used
unit1.pas(88,87) Error: Identifier not found "MB_YesNo"
unit1.pas(88,104) Error: Identifier not found "MB_IconQuestion"
unit1.pas(88,114) Error: Identifier not found "IdYes"
unit1.pas(91,17) Error: Identifier not found "CanClose"
unit1.pas(196) Fatal: There were 4 errors compiling module, stopping
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Choose whether to close the program
« Reply #5 on: July 29, 2010, 03:10:09 pm »
Read carefuly... you have to add unit LclType to uses.

Code: [Select]
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, LclType; 
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

xinyiman

  • Hero Member
  • *****
  • Posts: 2257
    • Lazarus and Free Pascal italian community
Re: Choose whether to close the program
« Reply #6 on: July 29, 2010, 03:13:55 pm »
Error CanClose  :o

Hint: Start of reading config file C:\lazarus\fpc\2.4.0\bin\i386-win32\fpc.cfg
Hint: End of reading config file C:\lazarus\fpc\2.4.0\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 2.4.0 [2010/07/19] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Win32 for i386
Compiling project1.lpr
Compiling unit1.pas
unit1.pas(29,24) Hint: Parameter "Sender" not used
unit1.pas(28,27) Hint: Parameter "Sender" not used
unit1.pas(26,30) Hint: Parameter "Sender" not used
unit1.pas(91,17) Error: Identifier not found "CanClose"
unit1.pas(201) Fatal: There were 1 errors compiling module, stopping
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2257
    • Lazarus and Free Pascal italian community
Re: Choose whether to close the program
« Reply #7 on: July 29, 2010, 03:17:29 pm »
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
     if Application.MessageBox('Are you sure you want to quit?', 'Question', MB_YesNo + MB_IconQuestion) = IdYes then
        Application.Terminate
     else
       CanClose:=FALSE;
end;

Error!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2257
    • Lazarus and Free Pascal italian community
Re: Choose whether to close the program
« Reply #8 on: July 29, 2010, 03:23:26 pm »
resolved


procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
     if Application.MessageBox('Are you sure you want to quit?', 'Question', MB_YesNo + MB_IconQuestion) = IdYes then
        Application.Terminate
     else
        CanClose := FALSE;
end;       
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Choose whether to close the program
« Reply #9 on: July 29, 2010, 03:33:52 pm »
Yes, you should place that snippet of code to CloseQuery event of the TForm because of using CanClose.
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

 

TinyPortal © 2005-2018