views:

1465

answers:

2

In my Delphi application I have a custom Yes, No, Cancel dialogue, which will be called from the main form to confirm saving the changes made to the current file edited. This would normally be achieved by messageDlg() but I wanted this dialogue to have customised looks, so I am trying to achieve this functionality by

case myDialogue.showModal of
  mrYes: <<save changes>>;
  mrNo: <<quit application without saving changes>>
  mrCancel: <<set the closeAction to caNone and do nothing>>
end;

The problem is that, by default, the form reacts to pressing the Escape key by returning mrNo TModalResult. And you can see how BAAAD this is, since your intuition tells you that Esc-aping the modal dialogue will CANCEL the intended Quit Application process, but in fact what happens is you issue a Don't save any changes command and application quits.

I have not noticed this behaviour until I lost an hour's work in this fashion. No FormKeyPressed event handler or anything responding to key presses was put into the myModalDialogue code. It just so works that pressing the Esc in forms shown using showModal will return mrNo. How can I override this default behaviour?

+14  A: 

You need to make sure that the Cancel property of the "No" button is False, and that the Cancel property of the "Cancel" button is True.

mghie
this worked. Thanks!
Peter Perháč
+4  A: 

Set the Cancel and Default properties of the buttons in your dialog.

anon
+1 for a correct answer. Thanks, I can't believe it was so simple... And I was going nuts trying to fix this...
Peter Perháč