Easy question : How to let messagDlg respond to cancelclicks on closing form?

Hi there!

This should be an easy one :
In my application I use the messagedlg(...) to ask for user confirmation when they try to close the form (are you sure? Yes/No)

Now when I close the form through the FileMenu (TMainMenu->Exit) the messagedlg responds to the cancelclick.

But when I press the close-cross (in top-right corner) the popu doesn't respond to cancelclicks, and just blatantly closes the entire application.

Is there a big difference, and/or am I forgetting something?

btw, here's the messageDlg part I used :

if MessageDlg('This will close the program',mtConfirmation,mbOk,      mbCancel], 0) = mrOk then
  Application.Terminate;// The end of the progame.
else ;  // Empty statement to let program respond to cancelclick

I think it has to do with the empty statement, is there another way to do this?

Thanks in advance

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you should place the dialog in the onclosequery-event
of your mainform and modify the canclose-paramater to your
needs -> in case of cancel -> canclose := false;

further advice:

in your exit-menu-click-event just place only the close-method

supply the onclosequery event like

canclose := MessageDlg('This will close the program',mtConfirmation,mbOk,      mbCancel], 0) = mrOk ;  

move this like
into the formdestroy-event of the main-form


meikl ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
or try using this :

procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;

procedure TForm1.WMSysCommand;
  if (Msg.CmdType=SC_ClOSE) then
    // show your dialog

or to disable the 'x'-button :

procedure TForm1.FormCreate(Sender: TObject);
  hMenuHandle: Integer;
  hMenuHandle := GetSystemMenu(Handle, False);
  if (hMenuHandle <> 0) then
    DeleteMenu(hMenuHandle, SC_CLOSE, MF_BYCOMMAND);

BinskyAuthor Commented:
Hi Kretzschmar! (and others of course...)

It took me a bit to find out exactly what you meant, (I wasn't reading it right, it was still a bit too much of the monday morning I guess) but that was exactly it!

I was trying to add your idea to the onclose event, which was essentially the same as what I had...But after my lunchbreak I actually saw what you wrote down...Tried it, and it worked (but you knew that!) The onclose will always close, except if the canclose bool is false. Good trick!

I also found out why my mainmenu=>exit_click did work, it didn't call the onclose, which windows "buttons" do!

Another day, another lesson!

Thankx, the 50 points are yours.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

BinskyAuthor Commented:
(I did it the wrong way around I guess, read comment above for actual comment.)
?? confused

well glad you got it work,
even i don't know yet, how

meikl ;-)
mondays are really hard days for me too :-))
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.