More Control of The MessageDlg

The MessageDlg function is very useful because it handles all wordwrapping of the message and handles positioning of the buttons. But two different needs for a non-default behavoir of the MessageDlg made me explore the sourcecode of the Dialogs unit.

This article originally appeared on

First I needed a modal dialogbox that would close when a key was pressed and report back what key was pressed. Much like the TurboPascal commands IF KEYPRESSED THEN TheKey:=READKEY.

Second I needed to control which of the buttons in the dialog was default. If you have a MessageDlg asking "Do you really want to delete the file?" then having the Yes-button as default (which is the standard in MessageDlg) could create problems for the much-to-eager user.

The Dialogs unit has a function called CreateMessageDialog which is used by MessageDlg to make the dialog. By using CreateMessageDialog directly you get the best of MessageDlg (wordwrapping and other automatic designelements) plus you get more control of the behavior of the dialog.

Below is shown my solution to the two needs.

Create a new application. Put two buttons and a label on the form. Assign the onClick event of button1 to TForm1.Button1Click and assign the onClick event of button2 to TForm2.Button2Click.

Clicking Button1 will now produce a dialog that closes when the user presses one of the "legal keys" or when the user presses ESC or clicks the close-icon. The key that was pressed will be shown in Label1.

Clicking Button2 will show a standard message dialog with one difference: the No-button is default, not the Yes-button. To find the No-button I use IF TButton(Controls[n]).Name='No'. Substitute 'No' with 'YesToAll','Abort' or any of the other button-captions to make a button default.

unit Unit1;



  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;


  TForm1 = class(TForm)

    Button1: TButton;

    Label1: TLabel;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure HelpBoxKeyPress(Sender: TObject; var Key: Char);

    procedure Button2Click(Sender: TObject);


    { Private declarations }


    { Public declarations }



  Form1: TForm1;


{$R *.DFM}


  LegalKeys: String;

procedure TForm1.Button1Click(Sender: TObject);





  s:='What do you want to do now?' + #13#13 +

    'A. Go home' + #13 +

    'B. Eat lunch' = #13+

    'C. Sleep' + #13 +

    '1. Program in Delphi' + #13 +

    '2. Order a Pizza' + #13 +

    '3. Nothing';

  WITH CreateMessageDialog(s, mtConfirmation, []) DO




      IF ShowModal=mrOK

      THEN Label1.Caption:='You pressed: '+LegalKeys[Tag]

      ELSE Label1.Caption:='You pressed: '+'Cancel';



    end; //try..finally

  END; //with


procedure TForm1.HelpBoxKeyPress(Sender: TObject; var Key: Char);


  IF Pos(UpCase(Key),LegalKeys)>0 THEN





  ELSE IF Key=#27 THEN TForm(Sender).ModalResult:=mrCancel;


procedure TForm1.Button2Click(Sender: TObject);




  WITH CreateMessageDialog('Do you really want to delete the file?',mtWarning,[mbYes,mbNo]) DO


      FOR n:=0 TO ControlCount-1 DO

        IF (Controls[n] is TButton)

        THEN IF TButton(Controls[n]).Name='No' THEN TButton (Controls[n]).TabOrder:=0;






Share this article!

Follow us!

Find more helpful articles: