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.

Setting a Default Result

You can see from the styling differences of the buttons in the above image that the "Yes" answer is the default option. If the user presses the Enter key, it will be the equivalent of clicking "Yes". In the example message box, this could be a problem. For example, it may be that exiting the software will cause data to be lost. In such cases, it would be more appropriate to set the "No" button as the standard option.

You can change the default button by passing an argument of the MessageBoxResult enumeration, which is the same type as is returned by Show. If the value passed in matches one of the buttons, that button is marked as the default.

Let's change the code so that the "No" button is preferred:

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

Run the program to see that the default button has changed.

WPF MessageBox with alternative default button

Message Box Options

There are several other options that you can use to customise the appearance and functionality of a message box. These are added using a MessageBoxOptions value. This enumeration uses the Flags attribute, so its options can be combined using a logical OR operation should you wish to apply more than one option at a time. There are four values in addition to the default of None:

  • RightAlign. Specifies that the text in the message box should be right-aligned. This formatting is applied to both the message and the title bar text.
  • RtlReading. All of the elements of the message box, including the image and the buttons, are displayed from right to left, instead of left to right.
  • DefaultDesktopOnly. This option is used when displaying a message box from a Windows service. It indicates that the message box should be displayed on the default desktop of the current workstation. This option is rarely used as services usually do not benefit from message boxes. If no user is logged in, the message box will not be visible.
  • ServiceNotification. This option is also used for displaying messages from Windows services. In this case, the message box will be displayed, even if no user is currently logged in.

Update the call to the Show method to add an option. The code below adds right-alignment to the text.

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

The message box's text now looks quite different.

WPF MessageBox with message box options applied

12 July 2015