MessageDlg change default button
How about creating a message box with the typical Yes-No buttons, but we want to:
- keep the order of buttons as it’s done everywhere;
- do not want to change the question so Yes becomes No and vice versa;
- 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();
This entry was posted on February 14, 2007 at 10:04 am and is filed under delphi, messagebox. You can subscribe via RSS 2.0 feed to this post's comments. You can comment below, or link to this permanent URL from your own site.
July 16, 2007 at 3:15 pm
Very helpful, Thanks!
February 21, 2008 at 12:49 pm
Excellent ! Et pourtant si simple…
February 21, 2008 at 12:53 pm
Thanks for Your feedback and nice to hear that it helped someone out there š
May 15, 2008 at 10:40 pm
One of the best and simplest tips I have seen – many thanks!
May 28, 2008 at 12:12 am
blackboard says : I absolutely agree with this !
August 7, 2008 at 10:30 am
had the same problem – thanks for the code example – saved me a headache !
February 27, 2009 at 4:41 am
I encountered same issue. This works brilliantly. Cheers.
March 9, 2009 at 3:17 am
Very nice tip!, THANK!
June 15, 2009 at 8:54 am
thank u very much
December 2, 2009 at 7:00 pm
thank you soooo much
September 4, 2010 at 3:12 pm
Thank you !!!
September 14, 2010 at 5:33 pm
I agree also. Well done and thanks!
April 30, 2011 at 6:50 pm
Very nice tip, saved me a lot of time š
November 21, 2011 at 3:00 pm
Well done. Very useful tip. Thanks.
December 12, 2011 at 12:25 pm
Very nice and useful tip. Thanks for this.
January 15, 2012 at 2:33 pm
but in Modalshow Form
that MessageBox can’t be Modal message that cause lots trouble
cause we still can active the modal form
January 15, 2012 at 3:09 pm
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
May 10, 2012 at 8:23 pm
Good one:) Thanks
February 26, 2013 at 2:12 am
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! š
July 23, 2014 at 2:42 pm
Does not work with Delphi 6
May 12, 2017 at 11:16 am
Nice trick !
It works perfectly for me š
March 1, 2021 at 1:29 am
14 years later now and at Delphi 10.4 Sydney. It still works!.