Recent

Author Topic: [SOLVED]Program Close everytime  (Read 3838 times)

Jeppe

  • Newbie
  • Posts: 2
[SOLVED]Program Close everytime
« on: July 03, 2017, 12:10:30 am »
I have a problem. My program close everytime when I hit no in the following code:


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. var
  3.   Antwort, BoxStyle : integer;
  4. begin
  5.   BoxStyle:=MB_ICONQUESTION + MB_YESNO;
  6.   Antwort:=Application.MessageBox('Willst du das Programm wirklich beenden?', 'Programm Beenden?', BoxStyle);
  7.  
  8. //  if Antwort = IDYES then
  9. //  begin
  10. //    Application.MessageBox('Ja', 'Antwort',MB_ICONINFORMATION)
  11. //  end
  12. //  else
  13. //  begin
  14. //    Application.MessageBox('Nein', 'Antwort', MB_ICONHAND);
  15. //  end;
  16.  
  17. //  Form1.Enabled:=false;
  18.  
  19.   if (Antwort = IDYES) and (B1.Visible = false) then
  20.   begin
  21.     Application.Terminate;
  22.   end
  23.   else if (Antwort = IDYES) and (B1.Visible = true) then
  24.   begin
  25.     BeendenKA;
  26.   end
  27.   else
  28.   begin
  29.     //Application.MessageBox('Super!', 'Antwort', MB_ICONINFORMATION);
  30.   end;
  31. end;



Does anyone know how I can fix that? I think that it's an easy way to solve but i'm a bit new in pascal programming.
« Last Edit: July 03, 2017, 07:34:10 pm by Jeppe »

paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: Program Close everytime
« Reply #1 on: July 03, 2017, 12:41:24 am »
Move code to the OnCloseQuery procedure and set CanClose: true (close) or false (don't close app)
Best regards / Pozdrawiam
paweld

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Program Close everytime
« Reply #2 on: July 03, 2017, 01:02:10 am »
Here is something easy...

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClose(Sender: TObject; Var CloseAction: TCloseAction);
  2.  Begin
  3.   Case
  4.    QuestionDlg('MY PROGRAM', 'QUIT ?', mtCustom,[mrYes, 'YES',
  5.                                                  mrNo,  'NO', 'ISDEFAULT'], '')
  6.   Of
  7.    mrYES   : Exit;
  8.    mrNO    : CloseAction:= caNone;
  9.    mrCANCEL: CloseAction:= caNone;
  10.   End;
  11.  End;

EDIT:
In your case maybe this... (don't know what you really want to do...)
Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2.  {$MODE OBJFPC}{$H+}
  3.  
  4. Interface
  5.  USES
  6.   Classes,  SysUtils, Forms,
  7.   Controls, Dialogs,  StdCtrls;
  8.  
  9.  TYPE
  10.   TForm1 = Class(TForm)
  11.  
  12.    B1: TButton;
  13.  
  14.    Procedure B1Click   (Sender: TObject);
  15.    Procedure FormClick (Sender: TObject);
  16.    Procedure FormClose (Sender: TObject; Var CloseAction: TCloseAction);
  17.   End;
  18.  
  19.  VAR
  20.   Form1: TForm1;
  21.  
  22. Implementation
  23.  {$R *.LFM}
  24.  
  25.  
  26. Procedure TForm1.FormClose(Sender: TObject; Var CloseAction: TCloseAction);
  27.  Begin
  28.   Case
  29.    QuestionDlg('MY PROGRAM', 'QUIT ?', mtCustom,[mrYes, 'YES',
  30.                                                  mrNo,  'NO', 'ISDEFAULT'], '')
  31.   Of
  32.    mrYES   :
  33.     Begin
  34.      If Not B1.Visible
  35.      Then
  36.       Begin
  37.        ShowMessage('Terminate');
  38.        Application.Terminate
  39.       End
  40.      Else Exit; // BeendenKA
  41.     End;
  42.    mrNO    : CloseAction:= caNone;
  43.    mrCANCEL: CloseAction:= caNone;
  44.   End;
  45.  End;
  46.  
  47.  
  48. Procedure TForm1.B1Click(Sender: TObject);
  49.  Begin
  50.   B1.Hide;
  51.  End;
  52.  
  53.  
  54. Procedure TForm1.FormClick(Sender: TObject);
  55.  Begin
  56.   B1.Show;
  57.  End;
  58.  
  59. END.
« Last Edit: July 03, 2017, 01:22:48 am by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Jeppe

  • Newbie
  • Posts: 2
Re: Program Close everytime
« Reply #3 on: July 03, 2017, 07:31:43 pm »
Thanks worked for me  :D

 

TinyPortal © 2005-2018