MessageDlg change default button

How about creating a message box with the typical Yes-No buttons, but we want to:

  1. keep the order of buttons as it’s done everywhere;
  2. do not want to change the question so Yes becomes No and vice versa;
  3. wanna make the No button as the default button for users who keep pressing Enter/Space for every question asked.

Even though Delphi 7’s helpfile shows that we can set a default button in a message dialog when using MessageDlg() function

function MessageDlg(const Msg: WideString; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint; DefaultBtn: TMsgDlgBtn = mbNone; Bitmap: TBitmap = nil): Integer; overload;

and there’s even an example which uses it

if MessageDlg(‘Welcome to my Delphi application. Exit now?’,
mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrYes then

but it turns out that these overloaded methods are implemented in QDialogs and not in Dialogs.

Easiest way to solve my problem was to use TApplication.MessageBox() method:

Use MessageBox to display a generic dialog box a message and one or more buttons.

And the code is something like:

if Application.MessageBox(
  PChar('Do You want to do something destructive today?'),
  PChar('Confirm'),
  MB_YESNO or MB_DEFBUTTON2 or MB_ICONQUESTION) = IDYES then
  DoSomethingDestructive()
else
  ForgetAboutIt();

Messagebox default button

Explore posts in the same categories: delphi, messagebox

22 Comments on “MessageDlg change default button”

  1. Paul Says:

    Very helpful, Thanks!

  2. Jean Francois DEVALEZ Says:

    Excellent ! Et pourtant si simple…

  3. toom Says:

    Thanks for Your feedback and nice to hear that it helped someone out there šŸ™‚

  4. Alan Says:

    One of the best and simplest tips I have seen – many thanks!

  5. blackboard Says:

    blackboard says : I absolutely agree with this !

  6. Peter Says:

    had the same problem – thanks for the code example – saved me a headache !

  7. Dave Says:

    I encountered same issue. This works brilliantly. Cheers.

  8. CHARIN Says:

    Very nice tip!, THANK!

  9. vinayak Says:

    thank u very much

  10. Lays Says:

    thank you soooo much

  11. Mario Says:

    Thank you !!!

  12. Wolfgang Says:

    I agree also. Well done and thanks!

  13. Lydeno Says:

    Very nice tip, saved me a lot of time šŸ™‚

  14. Mauricio Almeida Says:

    Well done. Very useful tip. Thanks.

  15. Dinesh Says:

    Very nice and useful tip. Thanks for this.

  16. zaki Says:

    but in Modalshow Form
    that MessageBox can’t be Modal message that cause lots trouble
    cause we still can active the modal form

    • zaki Says:

      if you do something like this:
      if MessageBox(0,
      PChar(‘Do You want to do something destructive today?’),
      PChar(‘Confirm’),
      MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONHAND) = IDYES then

  17. honest Says:

    Good one:) Thanks

  18. Marc Sykes Says:

    http://docwiki.embarcadero.com/CodeExamples/XE3/en/MessageDlg_(Delphi)

    here seen, seems like adding the mbYes / mbNo code AFTER zero ( 0) value right before the ) mark sets the default to whatever we add.
    So:
    if MessageDlg(‘Welcome to my Delphi application. Exit now?’,
    mtConfirmation, [mbYes, mbNo], 0, mbNo) = mrYes then

    In this case the No will be default, while this:

    if MessageDlg(‘Welcome to my Delphi application. Exit now?’,
    mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrYes then

    sets Yes to default! šŸ™‚

  19. REMY CONSTANT Says:

    Nice trick !
    It works perfectly for me šŸ™‚

  20. Peter Says:

    14 years later now and at Delphi 10.4 Sydney. It still works!.


Leave a reply to Alan Cancel reply