You are on page 1of 3

MessageDlg

Function Displays a message, symbol, and selectable buttons


Dialogs unit

function MessageDlg ( const Message : string; DialogType :


TMsgDlgType; Buttons : TMsgDlgButtons; HelpContext : Longint ) :
Integer;

Description

The MessageDlg function is used to display messages to the user. These messages may be
informational, or warnings or whatever. There is complete freedom over the choice of buttons that
the user may press to acknowledge the dialog.

For example, the user may be shown an error message, and be allowed to abort, retry or cancel the
erroneous process.

The DialogType may have one of the following enumerated values:

mtWarning Displays a exclamation symbol


mtError Displays a red 'X'
mtInformation Displays an 'i' in a bubble
mtConfirmation Displays an question mark
mtCustom Displays just the message

The Buttons value may be one or more of the following enumerated values :

mbYes Displays a 'Yes' button


mbNo Displays a 'No' button
mbOK Displays an 'OK' button
mbCancel Displays a 'Cancel' button
mbAbort Displays an 'Abort' button
mbRetry Displays a 'Retry' button
mbIgnore Displays an 'Ignore' button
mbAll Displays an 'All' button
mbNoToAll Displays a 'No to all' button
mbYesToAll Displys a 'Yes to all' button
mbHelp Displays a 'Help' button

You specify these values comma separated in square brackets, as in the second code example.

Delphi provides a number of predefined button combinations:

mbYesNoCancel = [mbYes,mbNO,mbCancel]
mbYesAllNoAllCancel =[mbYes,mbYesToAll, mbNo,mbNoToAll,mbCancel]
mbOKCancel =[mbOK,mbCancel]
mbAbortRetryCancel =[mbAbort,mbRetry,mbCancel]
mbAbortIgnore =[mbAbort,mbIgnore]

Now Delphi seem to have made a design error when setting the return value from the dialog box.
Instead of specifying the enumeration value of the button pressed, it uses a completely different set
of enumeration names:

mrYes = 6
mrNo = 7
mrOK = 1
mrCancel = 2
mrAbort = 3
mrRetry = 4
mrIgnore = 5
mrAll = 8
mrNoToAll = 9
mrYesToAll = 10

The values given are the numerical values of these enumerations, given in the numerical order that
the mb equivalents are defined. This is very odd.

Additionally, these values are defined in the Controls unit, not the Dialogs unit.

Note that the Help button has no equivalent return value. This is because it does not terminate the
dialog.

The HelpContext value is used in conjunction with the Help button. It's use is beyond the scope of
Delphi Basics.
Example code : Display a confirmation dialog

var
buttonSelected : Integer;
begin
// Show a confirmation dialog
buttonSelected := MessageDlg('Confirmation',mtError, mbOKCancel, 0);

// Show the button type selected


if buttonSelected = mrOK then ShowMessage('OK pressed');
if buttonSelected = mrCancel then ShowMessage('Cancel pressed');
end;
Show full unit code

A confirmation dialog is displayed with OK and Cancel buttons.


The user presses OK :

OK pressed

is displayed in another dialog box.

Example code : Displays a custom dialog with custom button selection


var
buttonSelected : Integer;
begin
// Show a custom dialog
buttonSelected := MessageDlg('Custom dialog',mtCustom,
[mbYes,mbAll,mbCancel], 0);

// Show the button type selected


if buttonSelected = mrYes then ShowMessage('Yes pressed');
if buttonSelected = mrAll then ShowMessage('All pressed');
if buttonSelected = mrCancel then ShowMessage('Cancel pressed');
end;
Show full unit code
A dialog with no icon is displayed with Yes, Cancel and All buttons.
The user presses the All button :

All pressed

is shown in another dialog

You might also like