RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TForm.OnCloseQuery Event

Occurs when close is attempted.

Pascal
property OnCloseQuery: TCloseQueryEvent;
C++
__property TCloseQueryEvent OnCloseQuery;

Use OnCloseQuery to specify the conditions under which the form can close. An OnCloseQuery event handler returns a Boolean CanClose value that determines whether a form is allowed to close. Its default value is true.  

You can use an OnCloseQuery event handler to ask users if they are sure they really want the form closed immediately. For example, you can use the handler to display a message box that prompts the user to save a file before closing the form. 

The TCloseQueryEvent type points to the method that determines whether a form can be closed. The value of the CanClose parameter determines if the form can close or not.  

C++ Examples: 

 

/*
When the user attempts to close the form in this example, a
message dialog appears that asks the user if it is OK to
close the form. If the user chooses the OK button, the form
closes. If the user chooses Cancel, the form doesn't close.
Populate the OnCloseQuery event handler of the form.
*/
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
  if (MessageDlg("Close the form?", mtConfirmation, TMsgDlgButtons() << mbOK << mbCancel,0) == mrCancel)
    CanClose = false;
}

 

Delphi Examples: 

{
When the user attempts to close the form in this example, a
message dialog appears that asks the user if it is OK to
close the form. If the user chooses the OK button, the form
closes. If the user chooses Cancel, the form doesn't close.
Populate the OnCloseQuery event handler of the form.
}
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if MessageDlg('Close the form?', mtConfirmation,
    [mbOk, mbCancel], 0) = mrCancel then
     CanClose := False;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!