Icon Showing Message Dialogs

Message dialogs are critical in a visual client application for displaying important messages such as errors or warnings to users, as well as asking the user to answer important questions that determine the overall flow of processing. There are two procedures that provide the message dialog functionality:
  • ShowMessage - This procedure simply displays a message to the user using a modal dialog containing the message and a single OK button.


  • MessageDlg - This procedure displays a message to the user using a modal dialog containing the message and any number of user-configured buttons.
The ShowMessage procedure is the simplest to use when you only want to display a message to the user and do not need to ask the user to provide any further information. The following example shows how you would show such a message dialog:

procedure ShowTrialMessage;
begin
   if IsTrialVersion and (TrialDaysLeft > 0) then
      ShowMessage('You are using a trial version and have '+
                  IntToStr(TrialDaysLeft)' evaluation days '+
                  'left until your trial version expires.')
   else
      ShowMessage('Your trial version has unfortunately expired.');
end;

The MessageDlg procedure is more versatile, but also slightly more complicated. It allows you to specify various attributes of the modal dialog used to display the message such as the dialog caption, the type of dialog (determines the icon used for the dialog), which buttons to display on the dialog, and whether or not to display a dialog close button. The following example shows how you would use this procedure to ask the user to confirm the deletion of a customer order in a dataset:

procedure TMasterDetailForm.CheckDelete(DlgResult: TModalResult);
begin
   if (DlgResult=mrYes) then
      begin
      Database.StartTransaction;
      CustomerOrders.Delete;
      Database.Commit;
      end;
end;

procedure TMasterDetailForm.DeleteOrderButtonClick(Sender: TObject);
begin
   MessageDlg('Are you sure that you want to delete the '+
              CustomerOrders.Columns['OrderID'].AsString+' order placed on '+
              CustomerOrders.Columns['OrderDate'].AsString+' ?','Please Confirm',
              mtConfirmation,[mbYes,mbNo],mbNo,CheckDelete,True);
end;

Information The MessageDlg procedure is overloaded and has two different versions. The first does not include the default button parameter after the array of button types, whereas the second version (shown above) does include the default button parameter.

As discussed in the previous Creating and Showing Forms topic, modal forms require some special event handler logic in order to execute code when the modal form is closed. This is especially true with message dialogs, which are always shown modally, and is why the MessageDlg procedure accepts a method reference as a parameter. The method reference should point to a method that matches the following type:

TMsgDlgResultEvent = procedure (DlgResult: TModalResult) of object;

When the modal message dialog form is closed, the event handler will be called and the type of button that the user clicked in the message dialog will be passed as the first parameter.

Information The TModalResult message dialog result type is different from the button types (TMsgDlgBtn type) that are passed as an array parameter to the MessageDlg procedure. The two types are similar, but there are additional results such as mrNone, which indicates that the user closed the dialog without clicking on any button at all.
Image