TFormOnClose (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example displays a message dialog box when you attempt to close the form. If you click the Yes button, the form closes; otherwise, the form only minimizes. TCustomForm.Close does not allow an action of caMinimize for a form closing itself. Close Form2 from Form1 and implement the OnClose in Form2. Place "Application.CreateForm(TForm2, Form2);" in the project dpr file to open Form2 and set the Visible property of Form2 to True.

Code

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
var
  myYes, myNo: TMsgDlgBtn;
  myButs: TMsgDlgButtons;
begin
  myYes:= mbYes;
  myNo:= mbNo;
  myButs:= [myYes, myNo];
  if MessageDlg('Close application ?', mtConfirmation,
    myButs, 0) = mrYes then
    Action := caFree
  else
    Action := caMinimize;
end;

Uses