Jud 1 Posted August 22 I want to show a confirmation question before closing the main form by clicking on the "x" in the upper right corner. The following is based on Delphi help, the example given in a link in tform.OnCloseQuery help. It used to work in another program but it doesn't work in my current program. I put "procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);" in the form's class. Then I have this, based on the example: procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin if MessageDlg( 'Do you want to exit the program?', mtConfirmation, [mbYes, mbCancel], 0) = mrCancel then CanClose := false; end; The program starts and then I click a button to start some tasks. If I click on the "x" before the tasks are started, it closes immediately. If I do it after the tasks have started, it closes immediately if it is running from the EXE. If it is running from the IDE, it gives an access violation. What do I need to do to make it behave as intended (i.e. confirm before closing the form.) Share this post Link to post
JonRobertson 72 Posted August 22 21 minutes ago, Jud said: I put "procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);" in the form's class. Did you "put the procedure in the class" by double-clicking the event in the Object Inspector? Or did you manually add it to the class declaration? Share this post Link to post
Remy Lebeau 1392 Posted August 22 It sounds like you simply added the code manually to the Form's class in code, but you did not actually hook it up to the Form's UI. Go into the Object Inspector and make sure the OnCloseQuery event is actually assigned this procedure. And next time, don't add event handlers manually to your code, go into the Object Inspector and double-click on the desired event and let the IDE auto-generate the handler for you, then you can fill in its logic as needed. Share this post Link to post
Jud 1 Posted August 22 I added it manually because before when I clicked on "OnCloseQuery" in the form's inspector, it would not let me add it there, like the form OI usually does. But now it does, and is working. (I don't remember the error message I got when I tried c;licking on the event OnCloseQuery before.) Thanks for the replies! Share this post Link to post