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 Programming
.NET 1.1+

Windows Forms Message Boxes

Message boxes provide a simple, standardised method to display information, warnings and errors to the user. They can also be used to ask simple questions and to request confirmation of actions.

Adding an Icon

You can add an icon to a message box to give a visual cue to the severity of an error, or to indicate the type of action that you are requesting the user to take. To do so, add an extra argument to the call to Show, passing a value from the MessageBoxIcon enumeration. The enumeration includes a number of different icon options, though some show the same image.

ImageConstantsDescription
ErrorError / Stop / HandUsed when the message box indicates that an error occurred.
ExclamationExclamation / WarningUsed to show a warning.
InformationAsterisk / InformationUsed to indicate that the message box is showing only information.
QuestionQuestionUsed when the message box includes a question.
NoneShows no icon.

To demonstrate, let's add a question mark icon to the example message box.

private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = MessageBox.Show(
        "Save the document?", "Save?", MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);
    
    Text = result.ToString();
}

The message box now appears as shown below:

MessageBox with an Icon

Specifying a Default Button

If you look closely at the above image, you can see that the Yes button is the default option. If the user presses the Enter key before any other, the message box will close and return Yes as its result. You can change which button is the default by passing a further argument of the MessageBoxDefaultButton type. This has three constants, Button1, Button2 and Button3. They map to the buttons in the order in which they appear in the message box, so to make "No" the default, you can use Button2:

private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = MessageBox.Show(
        "Save the document?", "Save?", MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
    
    Text = result.ToString();
}

NB: If you use an invalid default button, such as Button3 when only two buttons are visible, the option is ignored.

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.

private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = MessageBox.Show(
        "Save the document?", "Save?", MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question, MessageBoxDefaultButton.Button2,
        MessageBoxOptions.RightAlign);
    
    Text = result.ToString();
}

The message box's text now looks quite different.

MessageBox with message box options applied

8 February 2016