[Lazarus] Modifying Form Event

Hans-Peter Diettrich DrDiettrich1 at aol.com
Thu Mar 13 13:51:48 CET 2014


Steve Gatenby schrieb:
> Hoping somebody may be able to explain why the following doesn't stop 
> the Form closing.
> 
> Button1 sets the method pointer correctly (I believe), as when closing 
> the form the showmessage occurs as expected (and shows the correct form 
> name).
> But the form closes anyway.
> 
> procedure MyFormCloseQuery(Sender: TObject; var CanClose: boolean);
> begin
>   ShowMessage('MyCloseQuery:'+TControl(Sender).Name);
>   CanClose := FALSE;
> end;
> 
> procedure TformMain.Button1Click(Sender: TObject);
> var
>   OnCloseQueryMethod            :TCloseQueryEvent;
> 
> begin
>   TMethod(OnCloseQueryMethod).Code := @MyFormCloseQuery;
>   TMethod(OnCloseQueryMethod).Data := formMain;
>   TForm(formMain).OnCloseQuery     := OnCloseQueryMethod;
> end;

Here formMain.OnCloseQuery := @MyFormCloseQuery should be sufficient, 
unless the OnCloseQuery handler deals with data in the form. But in this 
case the handler should be made part of the formMain code, so that it 
can access the form's fields and methods.

When your formMain closes anyway, I'd suggest to write an OnClose 
handler and set CloseAction to caNone there.

AFAIR OnCloseQuery is intended for ending an application, where the 
application denies ending when any form reports CanClose=False. Closing 
individual forms instead should be handled in OnClose.

DoDi





More information about the Lazarus mailing list