The MessageBox Explained

Windows Forms

The whole concept behind a message box is to display text to the user or to ask a question of some sort. So in practice we can use message boxes to display an error to the user, or maybe to ask the user to confirm something before continuing. A C# MessageBox is a read-only dialog box which means that the text displayed in the MessageBox cannot be edited by the end user.

With C#.NET it is very easy to display a message box. The following line of code will display the simplest form of message box available.

MessageBox.Show("Hello from daveoncsharp.com!");

MessageBox is the C# message box class which is part of the System.Windows.Forms namespace and Show() is the static method we must call in order to display the message box on screen. When we execute the code we would be shown a dialog which looks like the following when running under Microsoft Windows Vista:

Simple MessageBox

Adding a Caption

A message box caption is the text which is displayed in the title bar of the message box. To add a caption to our message box we have to pass the text we want to display as another parameter in the Show() method. In this example we are going to write “Welcome” in the caption.

MessageBox.Show("Hello from daveoncsharp.com!", "Welcome");

Customizing Buttons

The C# MessageBox class allows you to customize your message box’s functionality and style by assigning different buttons. There are six different button styles which can be used and they are shown below:

MessageBoxButtons Buttons Displayed
OK OK
OKCancel OKCancel
RetryCancel RetryCancel
YesNo YesNo
YesNoCancel YesNoCancel
AbortRetryIgnore AbortRetryIgnore

Now let’s change our simple message box to display the YesNo buttons instead of the default OK button. To do this we have to add a third parameter to the Show() method of the MessageBox class as shown below (Note I have split the MessageBox.Show() method over three lines. This does not affect the compiler because C# still regards it as a whole line.):

MessageBox.Show("Hello from daveoncsharp.com!",
                "Welcome",
                MessageBoxButtons.YesNo);

Setting the Icon

With C# you can choose to display a number of predefined icons on your message box or you may choose to not display an icon at all. Below is a list of the available icons.

MessageBoxIcon Icon
Asterisk asterisk
Error error
Exclamation exclamation
Hand hand
Information information
None
Question question
Stop stop
Warning warning

You can select which icon to display, if any, by using  the MessageBoxIcon enumeration and adding it as the next parameter to the Show() method of the MessageBox. This is shown below where we are displaying the Question icon.

MessageBox.Show("Do you want to continue?",
                "A Question",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);

Setting the Default Button

The default button is the currently selected or highlighted button, and if not manually set, the default button is decided by the operating system.The C# MessageBox accepts a parameter of type MessageBoxDefaultButton which is another enumeration and contains three values – Button1, Button2, and Button3. One of these values can be added as a parameter to the Show() method of the MessageBox class as seen below:

MessageBox.Show("Do you want to continue?",
                "A Question",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2);

Since our example message box is using MessageBoxButtons.YesNo, and therefore only has two buttons, we cannot make use of MessageBoxDefaultButton.Button3 as a third button does not exist. If we try and use Button3 in this situation, the value will be ignored and the default button will be set by the operating system.

So far our message box looks like this:

MessageBox

Capturing Button Return Values

Obviously, after you customize a message box’s buttons you will want to know which button the user clicked. This is very simple to do as the Show() method of the MessageBox class returns a value of type DialogResult which is another enumeration with the value of the selected button.

The below example is showing how different code can be executed according to which button the user selects.

if (MessageBox.Show("Do you want to continue?",
                    "A Question",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) == DialogResult.No)
{
    // The user clicked the 'No' button
}
else
{
    // The user clicked the 'Yes' button
}

Conclusion

This concludes my explanation of the simple properties of a C# MessageBox. There are a few other parameters which can be passed to the Show() method such as text alignment options for different cultures, and options to show a help button which links to a help file, but these are more advanced topics which I will be covering in the future.

Thanks for reading this article – I hope it was of some help. Please feel free to leave any comments below.

Stay tuned to this blog for more articles in the near future.

Dave

5 comments… add one

Leave a Comment