Archive for February 2007

MessageDlg change default button

February 14, 2007

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

Advertisements