RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Dialogs.MessageDlg Function

Displays a message dialog box in the center of the screen.

Pascal
function MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer; overload;
function MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint; DefaultButton: TMsgDlgBtn): Integer; overload;
C++
int MessageDlg(const AnsiString Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, Longint HelpCtx);
int MessageDlg(const AnsiString Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, Longint HelpCtx, TMsgDlgBtn DefaultButton);

Call MessageDlg to bring up a message box and obtain the user's response.  

Msg is the content of the message that appears. 

DlgType indicates the purpose of the dialog. 

Buttons indicates what buttons should appear in the message box. For a three button message box, use the syntax containing Button1, Button2, and Button3. 

Button1, Button2, and Button3 indicate what types of buttons to use for a three button message box. The resulting buttons appear in order. 

HelpCtx specifies the context ID for the help topic that should appear when the user clicks the help button or presses F1 while the dialog is displayed. 

DefaultBtn specifies which button from among those specified by Buttons (or Button1, Button2, and Button3) is the default button for the dialog. If DefaultBtn is mbNone, there is no default button. 

MessageDlg returns the value of the button the user selected. The following table lists the TMsgDlgBtn values for each type of button that can appear in the message box, and the corresponding value that is returned if the user selects that button:  

TMsgDlgBtn Value 
Corresponding return value 
mbOK  
mrOK  
mbCancel  
mbYes  
mrYes  
mbNo  
mrNo  
mbAbort  
mbRetry  
mbIgnore  
mbAll  
mrAll  
mbNoToAll  
mbYesToAll  
mbClose  

Note: If the user types Ctrl+C in the message box, the text of the message is copied to the clipboard.
 

C++ Examples: 

 

/*
The following code adds a new object to a list in a list
object and then removes it:
*/
#include <memory>       //for STL auto_ptr class

class TMyClass : public TComponent
{
__published:    // IDE-managed Components
private:    // User declarations
public:     // User declarations
  AnsiString MyString;
    __fastcall TMyClass(TComponent* Owner, AnsiString mystr);
};

__fastcall TMyClass::TMyClass(TComponent* Owner, AnsiString mystr)
    : TComponent(Owner)
{
  MyString = mystr;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::auto_ptr<TList> list(new TList);
  TMyClass *TheObject = new TMyClass(Form1, "This is an object."); // The owner will clean this up;
  list->Add(TheObject); // add AnsiString instance to list
  MessageDlg("The list has " + IntToStr(list->Count) + " objects",
    mtInformation, TMsgDlgButtons() << mbOK, 0);
  list->Remove(TheObject);
  MessageDlg("The list has " + IntToStr(list->Count) + " objects",
    mtInformation, TMsgDlgButtons() << mbOK, 0);
}

 

Delphi Examples: 

{
This example uses a button on a form. When the user clicks
the button, a message box appears, asking if the user wants
to exit the application. If the user chooses Yes, another
dialog box appears informing the user the application is
about to end. When user chooses OK or closes the dialog box,
the application ends.
} 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Dialogs.MessageDlg('Welcome to my Delphi application.  Exit now?',
    mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrYes then
  begin
    Dialogs.MessageDlg('Exiting the Delphi application.', mtInformation,
      [mbOk], 0, mbOk);
    Close;
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!