Using Message Dialogues

The function MessageDlg displays a message dialogue box in the centre of the screen. The function is declared as follows in Delphi 7.

function MessageDlg(const Msg: string; AType: TMsgDlgType; AButtons: TMsgDlgButtons; HelpCtx: Longint): Word;       

Call MessageDlg to bring up a message box and obtain the user's response. The message box displays the value of Msg, the string you pass to the function. Use the AType parameter to indicate the purpose of the dialogue. Use the AButtons parameter to indicate what buttons should appear in the message box. For example, [mbOK, mbCancel] puts the OK and Cancel buttons in the box. Set HelpCtx to zero if you are not supplying context-sensitive help.

The following table shows for Delphi 7 most of the possible values for the responses, the types of message dialogue and the buttons.

Return Values Message Types Buttons
mrAbort, mrAll, mrCancel, mrIgnore, mrOk, mrNo, mrNone, mrRetry, mrYes mtConfirmation, mtError, mtInformation, mtWarning mbAll, mbAbort, mbCancel, mbIgnore, mbNo, mbOK, mbRetry, mbYes

In Lazarus you can see the options available from the definitions of the types in the Dialogs unit:

type
  TMsgDlgType = (mtWarning, mtError, mtInformation, mtConfirmation, mtCustom);
  TMsgDlgBtn = (mbYes, mbNo, mbOK, mbCancel, mbAbort, mbRetry, mbIgnore,
                mbAll, mbNoToAll, mbYesToAll, mbHelp, mbClose);
  TMsgDlgButtons = set of TMsgDlgBtn;  

For our demonstration (using Lazarus) we added a radiogroup, button, and memo to the form. We double clicked on the radiogroup in the design form to obtain its editor, in which we added the names of our four options. The vital ItemIndex property of the radiogroup is zero-based. The screenshot shows the output after (i) selecting the confirmation, (ii) clicking on the View Dialog button, (iii) selecting Cancel and (iv) clicking again on View Dialog.

Output from program MsgDialogDemo

Output from program MsgDialogDemo

Your code would normally, of course, do more on receiving a response than output messages.

The essential code of the files needed for program MsgDialogDemo follows. You can download the three files for Lazarus in form_msg_dialog_demo.zip.

uMsgDialogDemo.pas

unit uMsgDialogDemo;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls;

type
  TfrmMsgDialog = class(TForm)
    btnView: TButton;
    memResponse: TMemo;
    rgMsgDialog: TRadioGroup;
    procedure btnViewClick(Sender: TObject);
  end; 

var
  frmMsgDialog: TfrmMsgDialog;

implementation

{$R *.lfm}

procedure TfrmMsgDialog.btnViewClick(Sender: TObject);
var
  Response : integer;
begin
  case rgMsgDialog.ItemIndex of
    0 : begin
          Response := MessageDlg('You have pressed the button. Be careful!', mtWarning, [mbOK], 0);
          if Response = mrOK then
            memResponse.Lines.text := 'Remember our warning!';
        end;
    1 : begin
          Response := MessageDlg('That was a mistake!', mtError, [mbOK, mbIgnore], 0);
          if Response = mrOK then
            memResponse.Lines.text := 'You acknowledged the error.';
          if Response = mrIgnore then
            memResponse.Lines.text := 'Ignore this at your peril!';

        end;
    2 : begin
          Response := MessageDlg('This in an information dialogue!', mtInformation, [mbOK], 0);
          if Response = mrOK then
            memResponse.Lines.text := 'You clicked on OK.';
        end;
    3 : begin
          Response := MessageDlg('Do you really want to continue?', mtConfirmation, [mbYes, mbNo, mbCancel], 0);
          case Response of
            mrYes : memResponse.Lines.text := 'You may continue.';
            mrNo : memResponse.Lines.text := 'We''ll pull the plug.';
            mrCancel : memResponse.Lines.text := 'Press the button again and decide.';
          end;
        end;
  end;
end;

end.

uMsgDialogDemo.lfm

object frmMsgDialog: TfrmMsgDialog
  Left = 557
  Height = 133
  Top = 281
  Width = 283
  Caption = 'Message Dialogues'
  ClientHeight = 133
  ClientWidth = 283
  LCLVersion = '0.9.30'
  object rgMsgDialog: TRadioGroup
    Left = 1
    Height = 105
    Top = 16
    Width = 127
    AutoFill = True
    Caption = 'Type'
    ChildSizing.LeftRightSpacing = 6
    ChildSizing.TopBottomSpacing = 6
    ChildSizing.EnlargeHorizontal = crsHomogenousChildResize
    ChildSizing.EnlargeVertical = crsHomogenousChildResize
    ChildSizing.ShrinkHorizontal = crsScaleChilds
    ChildSizing.ShrinkVertical = crsScaleChilds
    ChildSizing.Layout = cclLeftToRightThenTopToBottom
    ChildSizing.ControlsPerLine = 1
    ClientHeight = 83
    ClientWidth = 123
    Items.Strings = (
      'Warning'
      'Error'
      'Information'
      'Confirmation'
    )
    TabOrder = 0
  end
  object btnView: TButton
    Left = 141
    Height = 25
    Top = 96
    Width = 127
    Caption = 'View Dialogue'
    OnClick = btnViewClick
    TabOrder = 1
  end
  object memResponse: TMemo
    Left = 129
    Height = 64
    Top = 26
    Width = 150
    TabOrder = 2
  end
end    

MsgDialogDemo.lpr

program MsgDialogDemo;

uses
  Interfaces, Forms, uMsgDialogDemo;

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TfrmMsgDialog, frmMsgDialog);
  Application.Run;
end. 
Programming - a skill for life!

How to use message, file, colour, font and calendar dialogue boxes to interact with the user