BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Message Boxes

The one hundred and seventy-second part of the Windows Presentation Foundation Fundamentals tutorial examines message boxes. These are simple dialog boxes that display a message or a question and allow the user to respond by clicking a button.

Selecting the Displayed Buttons

Unless you specify otherwise, message boxes include a single, "OK", button. WPF message boxes support several different options for the buttons that are displayed. To change the buttons, you can add another argument to the method call. This should be a value from the MessageBoxButton enumeration. Four values are permitted:

  • OK. The default option. Only the "OK" button is displayed.
  • OKCancel. The "OK" button is joined by a "Cancel" button. This button set is ideal when you wish to warn the user that they are about to start a long-running or destructive process. You can give them the option to continue or cancel.
  • YesNo. Two buttons are displayed with the captions, "Yes" and "No". This button set is used when you wish to ask the user a simple question. For example, "Do you want to save the document before exiting?"
  • YesNoCancel. The fourth option adds a "Cancel" button to the YesNo option. If the question asked was the one mentioned above, "Yes" might save and exit, "No" might close the application without saving and "Cancel" would leave the program open.

Let's change the code so that the message box asks a question:

MessageBox.Show("Are you sure you wish to exit?", "Sure?", MessageBoxButton.YesNo);

The message box now has "Yes" and "No" buttons, as shown below:

WPF MessageBox with Question

Identifying the Clicked Button

Once you are displaying more than one button in a message box, you will need to know which was clicked when the dialog closes. Handily, the Show method returns a value from the MessageBoxResult enumeration. This includes the constants, OK, Cancel, Yes and No, each representing a different button.

Let's obtain the return value and display it using the TextBlock in the main window. Replace the call to MessageBox.Show with the following two lines:

MessageBoxResult result = MessageBox.Show(
    "Are you sure you wish to exit?", "Sure?", MessageBoxButton.YesNo);
ButtonLabel.Text = result.ToString();

Run the program, display the message box and click one of the buttons to close it. The name of the button will be displayed in the main window.

Adding a Message Box Image

As with message boxes that may be shown by other Microsoft Windows applications, you can include an icon in your message boxes. This can make it easier for the user to determine the type of message being shown at a glance. Images are added with a parameter that accepts a value from the MessageBoxImage enumeration. There are eight options available, though several share the same image:

ImageConstantsDescription
ErrorError / Stop / HandUsed when the message box indicates an error condition.
ExclamationExclamation / WarningUsed to indicate a warning that requires attention but is less serious than an error.
InformationInformationUser to indicate that the message box provides information only.
QuestionQuestionUsed when the message box includes a question that requires a user's response.
NoneNo icon is displayed. This is the default option.

NB: The image used also determines the sound that will be played when the message box is shown. The actual sounds are controlled by the user, via the Control Panel.

Update the line that shows the message box to include a question mark image, as follows:

MessageBoxResult result = MessageBox.Show(
    "Are you sure you wish to exit?", "Sure?", MessageBoxButton.YesNo,
    MessageBoxImage.Question);

The dialog box now includes an icon:

WPF MessageBox with Image

12 July 2015