MessageDlgConfirmation (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows to you about how to use the function MessageDlg for making a Confirmation Dialog.

There are two techniques that can be used:

  1. If the condition is not complicated, you can use if ... then.
  2. If you have a complicated purpose, you can use case ... of instead.

Screenshot

Picture MessageDlg Confirmation Delphi XE2.png
Message Dialog - Confirmation

Picture MessageDlg Confirmation Delphi XE2 - OK.png
Message Dialog - OK

Picture MessageDlg Confirmation Delphi XE2 - Cancel.png
Message Dialog - Cancel

Technique

To make more effective in writing source code you have, please use one of technique below.

Technique 1: Using Case ... Of

By using this technique, your Message Dialog more interactive with simple way.

unit Unit1;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs, // Automatically added by IDE
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
case MessageDlg('Are you ready to learn Delphi in LUCCA?', mtConfirmation, [mbOK, mbCancel], 0) of
  mrOk:
    begin
    // Write code here for pressing button OK
    ShowMessage('Thank you. Please visit us soon.');
    end;
  mrCancel:
    begin
    // Write code here for pressing button Cancel
    ShowMessage('Don''t hesitate to contact us when you need more information.');
    end;
end;
end;

end.

Technique 2: Using If ... Then

This technique can be implemented for simple use.

Notes

  1. If you are using Delphi XE2, you have to use library Vcl.Dialogs or Dialogs in Interface or Implementation section. And for previous version, such as: Delphi XE, Delphi 2010, Delphi 2009, etc.., use library Dialogs.
  2. Normally, if you add a new VCL Form or VCL Frame to your project or just create it, library Dialogs already available. But, if you add a new Data Module or Unit, you have to add library Dialogs manually.

Uses

See Also