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?'),

Messagebox default button

Explore posts in the same categories: delphi, messagebox

21 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?’),

  17. honest Says:

    Good one:) Thanks

  18. Marc Sykes Says:

    here seen, seems like adding the mbYes / mbNo code AFTER zero ( 0) value right before the ) mark sets the default to whatever we add.
    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! šŸ™‚


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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: