More Control of The MessageDlg

The MessageDlg function is very useful because it handles all wordwrapping of the message and handles positioning of the buttons. But two different needs for a non-default behavoir of the MessageDlg made me explore the sourcecode of the Dialogs unit.

This article originally appeared on www.undu.com



First I needed a modal dialogbox that would close when a key was pressed and report back what key was pressed. Much like the TurboPascal commands IF KEYPRESSED THEN TheKey:=READKEY.



Second I needed to control which of the buttons in the dialog was default. If you have a MessageDlg asking "Do you really want to delete the file?" then having the Yes-button as default (which is the standard in MessageDlg) could create problems for the much-to-eager user.



The Dialogs unit has a function called CreateMessageDialog which is used by MessageDlg to make the dialog. By using CreateMessageDialog directly you get the best of MessageDlg (wordwrapping and other automatic designelements) plus you get more control of the behavior of the dialog.



Below is shown my solution to the two needs.



Create a new application. Put two buttons and a label on the form. Assign the onClick event of button1 to TForm1.Button1Click and assign the onClick event of button2 to TForm2.Button2Click.



Clicking Button1 will now produce a dialog that closes when the user presses one of the "legal keys" or when the user presses ESC or clicks the close-icon. The key that was pressed will be shown in Label1.



Clicking Button2 will show a standard message dialog with one difference: the No-button is default, not the Yes-button. To find the No-button I use IF TButton(Controls[n]).Name='No'. Substitute 'No' with 'YesToAll','Abort' or any of the other button-captions to make a button default.



unit Unit1;

interface



uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Label1: TLabel;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure HelpBoxKeyPress(Sender: TObject; var Key: Char);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public


    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.DFM}



VAR

  LegalKeys: String;



procedure TForm1.Button1Click(Sender: TObject);

VAR

  s:String;

begin

  LegalKeys:='ABC123';

  s:='What do you want to do now?' + #13#13 +

    'A. Go home' + #13 +

    'B. Eat lunch' = #13+

    'C. Sleep' + #13 +

    '1. Program in Delphi' + #13 +

    '2. Order a Pizza' + #13 +

    '3. Nothing';



  WITH CreateMessageDialog(s, mtConfirmation, []) DO

  BEGIN

    try

      onKeyPress:=HelpBoxKeyPress;

      IF ShowModal=mrOK

      THEN Label1.Caption:='You pressed: '+LegalKeys[Tag]

      ELSE Label1.Caption:='You pressed: '+'Cancel';

    finally

      Free;

    end; //try..finally

  END; //with

end;



procedure TForm1.HelpBoxKeyPress(Sender: TObject; var Key: Char);

BEGIN

  IF Pos(UpCase(Key),LegalKeys)>0 THEN

    BEGIN

      TForm(Sender).Tag:=Pos(UpCase(Key),LegalKeys);

      TForm(Sender).ModalResult:=mrOK;

    END

  ELSE IF Key=#27 THEN TForm(Sender).ModalResult:=mrCancel;

END;



procedure TForm1.Button2Click(Sender: TObject);

VAR

  n:Integer;

begin

  WITH CreateMessageDialog('Do you really want to delete the file?',mtWarning,[mbYes,mbNo]) DO

    BEGIN

      FOR n:=0 TO ControlCount-1 DO

        IF (Controls[n] is TButton)

        THEN IF TButton(Controls[n]).Name='No' THEN TButton (Controls[n]).TabOrder:=0;

      ShowModal;

    END;

end;



end.






 

Share this article!

Follow us!

Find more helpful articles: