Change button texts on message dialog

Today customer wanted me to change the typical ‘OK’/’Cancel’ on one of the message dialogs in our app. While browsing through Delphi help and Windows SDK I came up with the idea to use CreateMessageDialog() function:

CreateMessageDialog returns a dialog of the type specified by the DlgType parameter and with the buttons indicated by the Buttons parameter.

Note the … returns a dialog … part here: I now have control over the TForm the function produces and all I have to do, is change captions on the TButton components on this form:

...
var
  i: integer;
begin

  with CreateMessageDialog(
    'Do You want to do something destructive today?',
    mtConfirmation,
    [mbYes, mbNo]) do try
    // go through all the components on the form
    for i := 0 to ComponentCount - 1 do
      // if we have a button ...
      if Components[i] is TButton then
        // it's the 'Yes' button
        if TButton(Components[i]).ModalResult = 
          mrYes then
          TButton(Components[i]).Caption := 'Yeah!'
        else // otherwise it's the 'No' button
          TButton(Components[i]).Caption := 'Nope';
    // CreateMessageDialog() onlt creates the form
    if ShowModal() = mrYes then
      DoSomeThingDestructive()
    else
      ForgetAboutIt();

  finally
    Free();
  end;

...

Of course, You can use other combination of buttons (eg. mbYesNoCancel) when creating the dialog and the appropriate modal results when changing captions before showing it to user.

Here’s the result of the code snippet above:

Messagebox with changed button texts

Advertisements
Explore posts in the same categories: delphi, messagebox

One Comment on “Change button texts on message dialog”

  1. Mario Says:

    Thank you very much !!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: