Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

user should not be able to close form

Deutsch · English · Français

You can use the OnCloseQuery event of TForm to be notified when a form is about to be closed. To program such an event handler for a form, select the form in the Form Designer and click on the Events tab of the Object Inspector. Then double-click on the OnCloseQuery event (it is on row 9 of the listed events). The Lazarus IDE will generate a new skeleton event handler for you to complete, and the focus will move to the Source Editor where you can type appropriate code. Your code should set the value of the var parameter CanClose which is passed when the OnCloseQuery event is called. Setting CanClose to False prevents the form from closing. Setting CanClose to True allows the form to close.

For example, here is an over-simple example which prevents a form closing until an edit field on the form (named Edit1) has been at least partly filled:

Example:

pascal
uses
  Forms, ...;
  
  ...
  
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin 
  CanClose := (Length(Edit1.Text) > 0); 
end;
  
  ...

From the Free Pascal wiki, page "user should not be able to close form", as it stood on 2020-03-03 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .