Difference between revisions of "user should not be able to close form"

From Free Pascal wiki
Jump to navigationJump to search
m
Line 1: Line 1:
{{user should not be able to close form}}
+
{{How to prevent a user from closing a form}}
 
<br>
 
<br>
The procedure prevents the termination of the specific program, by the user.<br>
+
You can use the OnCloseQuery event of TForm to be notified when a form is about to be closed.
Therefor the [[TForm | form]] event OnCloseQuery must to be adapted.<br>
+
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.
<br>
+
Then double-click on the OnCloseQuery event (it is on row 9 of the listed events).
Beispiel:
+
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:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
uses
 
uses
Line 13: Line 18:
 
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
 
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
 
begin  
 
begin  
   CanClose:=false;  
+
   CanClose := (Length(Edit1.Text) > 0);  
 
end;
 
end;
 
    
 
    

Revision as of 15:11, 25 August 2014

Template:How to prevent a user from closing a form
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:

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