WPF Message Box


The WPF message box is a type of dialog.

It is easy to display a message box in a Windows Presentation Foundation (WPF) program. There are lots of different ways to do this however, and that is why we have a separate post on this topic. Message boxes are often used in debugging and testing your code. Often you need to know the operation completed and you might need to know the values returned. You can display those in a message box.

If you just need to display a message to the user then the code is simply MessageBox.Show(“hello”);. This doesn’t look great however. Adding a title to the message box is a good idea.

There is a descent tutorial on the message box over at WPF Tutorial.

For debugging purposes you may just want to display a simple text message like “Operation complete”. To do that, here is the code:

MessageBox.Show("Operation complete");

The 5 parameters are:

  • message
  • title
  • buttons
  • image
  • options

Buttons

Here is a list of the different buttons.

  • OK
  • OKCancel
  • YesNoCancel
  • YesNo

Images

Here is a list of the different images available.

  • Asterisk
  • Error
  • Exclamation
  • Hand
  • Information
  • None
  • Question
  • Stop
  • Warning

Below is a WPF code listiing that shows 4 messages boxes after you click the button. What’s different about this is that the message in the message box is similar to the actual code you use to create the message box.

using System.Windows;
namespace MessageBoxes
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string msg = "MessageBox.Show(\"...\")";
            MessageBox.Show(msg);

            msg = "MessageBox.Show(\"...\",\"My App\")";
            MessageBox.Show(msg,"My App");

            msg = "MessageBox.Show(\"...\",\"My App\", MessageBoxButton.OKCancel)";
            MessageBox.Show(msg, "My App", MessageBoxButton.OKCancel);

            msg = "MessageBox.Show(\"...\",\n\"My App\",\n MessageBoxButton.YesNoCancel, " +
                "\nMessageBoxImage.Question";
            MessageBox.Show(msg, "My App", MessageBoxButton.YesNoCancel, 
                MessageBoxImage.Question);
        }
    }
}

Returning a Result

Here is how we get the response from the user to respond to the particular button they pressed.

            msg = "MessageBox.Show(\"...\",\n\"My App\",\n MessageBoxButton.YesNoCancel, " +
                "\nMessageBoxImage.Question";
            MessageBoxResult result =  MessageBox.Show(msg, "My App", MessageBoxButton.YesNoCancel, 
                MessageBoxImage.Question);
            switch (result)
            {
                case MessageBoxResult.Yes:
                    MessageBox.Show("You said Yes", "My App");
                    break;
                case MessageBoxResult.No:
                    MessageBox.Show("You said No", "My App");
                    break;
                case MessageBoxResult.Cancel:
                    MessageBox.Show("You Cancelled", "My App");
                    break;
            }

Non-Default Options

The MessageBox will select a button as the default choice, which is then the button invoked in case the user just presses Enter once the dialog is shown. For instance, if you display a MessageBox with a “Yes” and a “No” button, “Yes” will be the default answer. You can change this behavior using a fifth parameter to the MessageBox.Show() method. The following code makes the Cancel button the default button

MessageBoxResult result =  MessageBox.Show(msg, "My App", MessageBoxButton.YesNoCancel, 
                MessageBoxImage.Question, MessageBoxResult.Cancel);

Message Box Default Button

Here below is some C# code.

    DialogResult result = MessageBox.Show(
        "The second button is the default.",
        "Caption",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Information,
        MessageBoxDefaultButton.Button2);