1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram

  5. Delphifan Magazine
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Source Code HOW TO customize a MessageDlg

Discussion in 'Delphi Programming' started by AdminDF, Jul 8, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member Delphifan Staff Member DF Staff

    You can use something like below to show a Dialog that will be closed automatically.

    Code:
    unit uAutoCloseDialog;
    
    interface
    
    uses
      Windows,
      Messages,
      SysUtils,
      Variants,
      Classes,
      Controls,
      Forms,
      Dialogs,
      StdCtrls,
      ExtCtrls;
    
    type
      TAutoCloseDialog = class
      private
        FDialog: TForm;
        FCheckBox: TCheckBox;
        FTimer: TTimer;
        FInfo: TLabel;
        FCountDown: Integer;
        FCounter: Integer;
        FModalResult: Integer;
        FShowCounter: Boolean;
        function GetButtonText(const Name: string): string;
        function GetCheckDontShow: Boolean;
        function GetTitle: string;
        procedure SetButtonText(const Name, Value: string);
        procedure SetTitle(const Value: string);
        procedure TimerClick(Sender: TObject);
      public
        constructor Create(const Text: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons);
        destructor Destroy; override;
        function Execute(): Boolean;
        property CountDown: Integer read FCountDown write FCountDown;
        property ModalResult: Integer read FModalResult;
        property Title: string read GetTitle write SetTitle;
        property CheckDontShow: Boolean read GetCheckDontShow;
        property ShowCounter: Boolean read FShowCounter write FShowCounter;
        property ButtonCaption[const Name: string]: string read GetButtonText write SetButtonText;
      end;
    
    implementation
    
    { TAutoCloseDialog }
    
    constructor TAutoCloseDialog.Create(const Text: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons);
    begin
      FDialog := CreateMessageDialog(Text, DlgType, Buttons);
    
      FDialog.Height := FDialog.Height + 20;
    
      FCheckBox := TCheckBox.Create(FDialog);
      FCheckBox.Parent := FDialog;
      FCheckBox.SetBounds(8, FDialog.ClientHeight - 17 - 8, 140, 17);
      FCheckBox.Caption := 'Don''t show me again.';
    
      FInfo := TLabel.Create(FDialog);
      FInfo.Parent := FDialog;
      FInfo.SetBounds(FDialog.ClientWidth - 38, FDialog.ClientHeight - 13 - 8, 30, 13);
      FInfo.Alignment := taRightJustify;
    
      FTimer := TTimer.Create(FDialog);
      FTimer.Enabled := False;
      FTimer.OnTimer := TimerClick;
      FCountDown := 10;
      FShowCounter := True;
    end;
    
    destructor TAutoCloseDialog.Destroy;
    begin
      FDialog.Free();
      inherited;
    end;
    
    function TAutoCloseDialog.Execute: Boolean;
    begin
      FCounter := FCountDown;
      FTimer.Enabled := FCounter > 0;
      if (FShowCounter) then
        FInfo.Caption := IntToStr(FCounter);
      FModalResult := FDialog.ShowModal();
      Result := FModalResult = mrYes;
    end;
    
    function TAutoCloseDialog.GetButtonText(const Name: string): string;
    var
      Control: TButton;
    begin
      Control := FDialog.FindComponent(Name) as TButton;
      if (not Assigned(Control)) then
        Exit('');
    
      Exit(Control.Caption);
    end;
    
    function TAutoCloseDialog.GetCheckDontShow: Boolean;
    begin
      Result := FCheckBox.Checked;  // Must be .Checked not .Enabled!  Thanks to yocko
    end;
    
    function TAutoCloseDialog.GetTitle: string;
    begin
      Result := FDialog.Caption;
    end;
    
    procedure TAutoCloseDialog.SetButtonText(const Name, Value: string);
    var
      Control: TButton;
    begin
      Control := FDialog.FindComponent(Name) as TButton;
      if (Assigned(Control)) then
        Control.Caption := Value;
    end;
    
    procedure TAutoCloseDialog.SetTitle(const Value: string);
    begin
      FDialog.Caption := Value;
    end;
    
    procedure TAutoCloseDialog.TimerClick(Sender: TObject);
    begin
      Dec(FCounter);
      if (FShowCounter) then
        FInfo.Caption := IntToStr(FCounter);
      if (FCounter = 0) then
      begin
        FTimer.Enabled := False;
        FDialog.ModalResult := mrYes;
      end;
    end;
    
    end.

    The using is simple.

    Code:
    procedure TForm1.Button2Click(Sender: TObject);
    var
      Text: string;
      Dialog: TAutoCloseDialog;
    begin
      Text := 'This is a test message. with multiple lines.'#13#10'Line 1.'#13#10'Line 2.'#13#10'Line 3.';
      Dialog := TAutoCloseDialog.Create(Text, mtConfirmation, mbYesNoCancel);
      try
        Dialog.ButtonCaption['Yes'] := 'Details';
        Dialog.ButtonCaption['No'] := 'Fat';
        Dialog.ButtonCaption['Cancel'] := 'Cancel';
        Dialog.Title := 'Warning';
        Dialog.CountDown := 5;
        if (Dialog.Execute()) then
        begin
          if (Dialog.CheckDontShow) then
          begin
            // Do whatever
          end;
        end;
      finally
        Dialog.Free();
      end;
    end;
    Another example.

    Code:
    procedure TForm1.Button2Click(Sender: TObject);
    var
      Text: string;
      Dialog: TAutoCloseDialog;
    begin
      Text := 'This is a test message. with multiple lines.'#13#10'Line 1.'#13#10'Line 2.'#13#10'Line 3.';
      Dialog := TAutoCloseDialog.Create(Text, mtConfirmation, mbYesNoCancel);
      try
        Dialog.ButtonCaption['Yes'] := 'Details';
        Dialog.ButtonCaption['No'] := 'Fat';
        Dialog.ButtonCaption['Cancel'] := 'Cancel';
        Dialog.Title := 'Warning';
        Dialog.CountDown := 5;
        Dialog.Execute();
    
        if (Dialog.CheckDontShow) then
        begin
          // Do whatever
        end;
    
        case Dialog.ModalResult of
          mrYes    : ShowMessage('Yes');
          mrNo     : ShowMessage('No');
          mrCancel : ShowMessage('Cancel');
        end;
      finally
        Dialog.Free();
      end;
    end;
     
  2. egeven
    Offline

    egeven DF Member

    RE:

    [font=Verdana, Arial, sans-serif]Thanks[/font]
     
  3. perdcomp
    Offline

    perdcomp DF Junior

    Thanks
     
  4. mato0o
    Offline

    mato0o DF Junior

    Thank you!
     
  5. peyman1
    Online

    peyman1 Guest

    RE:

    thanks
     
  6. topst7
    Offline

    topst7 DF Member

    RE:

    it interesting, thank you
     
  7. pggbs
    Online

    pggbs Guest

    RE:

    thank you
     
  8. ZOLO
    Online

    ZOLO Guest

    RE:

    So good.
     
  9. profepaco
    Offline

    profepaco DF Junior

    RE:

    very good. Thanks
     
  10. arabdent
    Online

    arabdent Guest

    RE:

    thanks
     
  11. Tallys
    Offline

    Tallys DF Junior

    Obrigado!
     
  12. david91
    Offline

    david91 DF Member

    Thanks!
     
  13. vcorp
    Offline

    vcorp DF Junior

    Another food idea to be added to my Real Rally Manager. Thanks for sharing it!
     
  14. george
    Online

    george DF Member

    power!
     
  15. toni_036
    Online

    toni_036 DF Member

    its useful
    thanks
     

Share This Page