Topic: Procedure Close On Form1

I have the following Procedure and I want when I press the Close ( X button on the right upper form )
when I add the name of the procedure (Telos) in the Form Events OnClose it doesn't work. How can I do it o show me  the messagebox from the procedure Telos?

Procedure Telos (Sender: TObject; var Cancel: boolean);   
       messagebeep(0);
         if IDNO = MessageBox('It will end , are you sure ?', 'Question', MB_YESNO+MB_ICONQUESTION) then // If the user clicked No
    begin
        Cancel := True; // If the Cancel parameter is set to True, the action that is assigned to the button will not be executed
        exit;
    end;

    frmApografi.Close;
end;

Re: Procedure Close On Form1

Hllo Poizidis
Try this, it's OK everywhere I use it in my projects :

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin                                                                             // mbYes est 6 - mbNo is 7 - mbCancel est 2
   If 7 = MessageDlg('Vous voulez quitter l''application ?', mtConfirmation, mbYes + mbNo, 0) then
      Cancel := True;
end;
JB

Re: Procedure Close On Form1

For me, it works with this formula:
-------------------------------------
procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin // mbYes is 6 - mbNo is 7 - mbCancel is 2

If 6 = MessageDlg('Do you want to quit the application?', mtConfirmation, mbYes + mbNo, 0) then

Form1.Close;

end;

Destiny

Re: Procedure Close On Form1

Hello All,
I don't think this can be done when clicking on the 'x' icon (top right of form) to close the form.  When this is clicked, the 'formclose' instruction has already been actioned and any message that you display (whether using 'messagebox' or 'messagedlg') is really just for information - it's already too late to cancel.
The only way would be to create your own 'formclose' button and then attach code to this (but then do you leave the 'x' icon in place and visible (rather confusing) or do you set the form 'border style' to 'bsnone' (which means you loose the ability to drag the form)?
All of the possible solutions are, unfortunately, a bit of a compromise to what V_Pozidis really wants.
Derek.

5 (edited by sparrow 2026-05-05 12:10:57)

Re: Procedure Close On Form1

Hi everyone.

Derek, you're right, this is indeed the case for MVD.
But in this case, you can work around this by assigning a new procedure to the form's OnCloseQuery event.
This was discussed in both the Russian and English forum threads.
This procedure will then be able to control closing by the Exit button, the "X" button, and Alt+F4.
It's declared as follows:
procedure Form1_OnCloseQuery(Sender: TObject; var CanClose: Boolean);

Re: Procedure Close On Form1

Hi Sparrow,
I did wonder about calling a bespoke procedure from the 'form1.onclose' event but I'd always thought that the 'form1.onclose' event would always run first before any other called procedure - but it seems that it's the opposite way round.
Knowing this now, I can see it being useful in quite a few scenarios.
So much still to learn sad
Thanks,
Derek.

Re: Procedure Close On Form1

Hi Derek

And don't be sad, we're always learning.
I really like your unconventional and simple approaches to solutions. This greatly expands the range of possible solutions or hints at other possible solutions.
Perhaps I misunderstood the text, but this is a separate event and isn't called from OnClose.
Let's look at a form (the TForm class):
Here's a link, for example:
https://www.thoughtco.com/life-cycle-of … rm-1058011
Let's read about the events a form has when creating and closing it, its sequences, and its capabilities. As we can see, the event I mentioned above occurs before OnClose.
Open this link:
https://docwiki.embarcadero.com/CodeExa … y_(Delphi)
Use example from a developer.
Similar examples can be found on other websites.
All that's left is to assign the procedure to the event in the MVD.
begin
Frm.OnCloseQuery := @procedurename;
end.
And here, the OnClose event isn't really needed for this form.
Unfortunately, new Pascal/Delphi features or component properties aren't always applicable in MVDs. Some things may have been changed by the MVD author, renamed or changed variables, or prohibited. But you can always try ))).

8 (edited by v_pozidis 2026-05-06 18:45:24)

Re: Procedure Close On Form1

Hi friends, jean.brezhonek, Destiny,Derek,sparrow I try the example but  does not work. It loops when adding it on Form1.OnClose
When adding the frm.OnCloseQuery := @procedurename; the Cancel do not work.

Re: Procedure Close On Form1

Hi,
Here's a working example - if you copy the code, it should be okay (or if not, attach your project to see what the problem might be).
Thanks to Sparrow for the solution.
Derek.

Post's attachments

Attachment icon ask on exit.zip 446.55 kb, 32 downloads since 2026-05-06 

Re: Procedure Close On Form1

derek and all my friends , a big thank you to all of you.  Every answerer is for me a new lesson.

Re: Procedure Close On Form1

Hello everyone, thank you Derek for your example. The code I presented uses a button because I hid the Form1 menu. Both examples work perfectly.

Destiny