Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-03

Modifying Form Event

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Steve Gatenby

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;

Thanks - SteveG

Reply by email

Mattias Gaertner
to Steve Gatenby
Show quoted text 9 lines
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);

Methods have a hidden Self parameter:

procedure MyFormCloseQuery(Self: TObject; Sender: TObject; var
CanClose: boolean);

Keep in mind that this is an implementation detail of the compiler, which may be different on other platforms, may not work with special compiler flags and may change in future.

Mattias

Reply by email

Hans-Peter Diettrich
to Steve Gatenby: Hoping somebody may be able to explain why the following doesn't stop the Form closing.…
Show quoted text 23 lines
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

Reply by email

Hans-Peter Diettrich
to Mattias Gaertner: On Thu, 13 Mar 2014 22:45:44 +1030 Methods have a hidden Self parameter: procedure…
Show quoted text 17 lines
On Thu, 13 Mar 2014 22:45:44 +1030 Steve Gatenby wrote: > 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); Methods have a hidden Self parameter: procedure MyFormCloseQuery(Self: TObject; Sender: TObject; var CanClose: boolean);

Ah, I missed *that* essential detail in my answer :-(

Thanks
DoDi

Reply by email

Mattias Gaertner
to Hans-Peter Diettrich: Here formMain.OnCloseQuery := @MyFormCloseQuery should be sufficient, unless the…
[...] Here formMain.OnCloseQuery := @MyFormCloseQuery should be sufficient,

Compiler will refuse that.

Show quoted text 7 lines
[...] 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.

OnCloseQuery defines if to close or not, OnClose defines how to close.

Mattias

Reply by email

Unknown author
to Mattias Gaertner: On Thu, 13 Mar 2014 22:45:44 +1030 Methods have a hidden Self parameter: procedure…
Show quoted text 30 lines
On Thu, 13 Mar 2014 22:45:44 +1030 Steve Gatenby wrote: > 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); Methods have a hidden Self parameter: procedure MyFormCloseQuery(Self: TObject; Sender: TObject; var CanClose: boolean); Keep in mind that this is an implementation detail of the compiler, which may be different on other platforms, may not work with special compiler flags and may change in future. Mattias -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

would there be any way to accomplish what I'm after then ? I am trying to indirectly prevent the form closing (by passing the Form to a Method / Proc)
So with no additions to the original forms class or instance.

Thanks

Reply by email

Hans-Peter Diettrich
to Unknown author
Show quoted text 14 lines
> On Thu, 13 Mar 2014 22:45:44 +1030 > Steve Gatenby wrote: > >> 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). would there be any way to accomplish what I'm after then ? I am trying to indirectly prevent the form closing (by passing the Form to a Method / Proc) So with no additions to the original forms class or instance.

You can add your handler to the form containing Button1. Then the handler is a method, ready for assignment to the OnFormClose of any form in your project.

DoDi

Reply by email

Unknown author
to Hans-Peter Diettrich
Show quoted text 21 lines
schrieb: >> On Thu, 13 Mar 2014 22:45:44 +1030 >> Steve Gatenby wrote: >> >>> 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). > would there be any way to accomplish what I'm after then ? > I am trying to indirectly prevent the form closing (by passing the Form > to a Method / Proc) > So with no additions to the original forms class or instance. You can add your handler to the form containing Button1. Then the handler is a method, ready for assignment to the OnFormClose of any form in your project.

Thanks Hans, will try that out

Steve

Show quoted text 9 lines
DoDi -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email