Friday, October 21, 2011
C# MessageBox.Show Examples in Windows Forms
You want to show a dialog box to the user in your Windows Forms program using the C# language. You may also need to get the result of the user's response to this dialog and act on it. The MessageBox.Show method in Windows Forms is ideal for this purpose, and can solve the problem very quickly. Here we look at many examples of using the MessageBox.Show method to show dialog boxes, using icons, buttons, default buttons, titles and text, and reading the input given by the user.
Examples
To start, the MessageBox.Show method is a static method, which means you do not need to create a new MessageBox() anywhere in your code. Instead, you can simply type "MessageBox" and press the period, and then select Show. The following example shows the MessageBox.Show method used in the Form1_Load event handler. To make the Form1_Load event handler, create a new Windows Forms application and double-click on the window in the designer.
Windows Forms program that uses MessageBox [C#]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//
// The simplest overload of MessageBox.Show. [1]
//
MessageBox.Show("Dot Net Perls is awesome.");
//
// Dialog box with text and a title. [2]
//
MessageBox.Show("Dot Net Perls is awesome.",
"Important Message");
//
// Dialog box with two buttons: yes and no. [3]
//
DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?",
"Important Question",
MessageBoxButtons.YesNo);
//
// Dialog box with question icon. [4]
//
DialogResult result2 = MessageBox.Show("Is Dot Net Perls awesome?",
"Important Query",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
//
// Dialog box with question icon and default button. [5]
//
DialogResult result3 = MessageBox.Show("Is Visual Basic awesome?",
"The Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
//
// Test the results of the previous three dialogs. [6]
//
if (result1 == DialogResult.Yes &&
result2 == DialogResult.Yes &&
result3 == DialogResult.No)
{
MessageBox.Show("You answered yes, yes and no.");
}
//
// Dialog box that is right-aligned (not useful). [7]
//
MessageBox.Show("Dot Net Perls is the best.",
"Critical Warning",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign,
true);
//
// Dialog box with exclamation icon. [8]
//
MessageBox.Show("Dot Net Perls is super.",
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
}
Overview of the example. In the Form1_Load method, there are eight calls to the MessageBox.Show method using different overloads. The Form1_Load method will be executed immediately after your Windows Forms program starts. So when you run the program, you will be shown all the dialogs in sequential order. The MessageBox.Show calls above call into different implementations of the function based on their parameter lists, which is called overload resolution.
Typing in the options. The easiest way to use MessageBox.Show is to type in "MessageBox", and then press period, and then select Show. Next, Visual Studio will show a popup with the overload list. You can scroll through the overload lists and try to find the best option. Next, type the parameters. For a parameter such as "MessageBoxButtons", type in "MessageBoxButtons" and press period to see all the options. You do not need to create a new MessageBoxButtons() object.
Parameter order. The order of the parameters in the MessageBox.Show method calls is important. The parameter order gives the compiler the ability to apply overload resolution to call the best method in the method group. See "Typing in the options" for tips on how to specify the options the easiest way.
Matching up the images. The image at the top of this document shows eight dialog boxes. These eight dialog boxes are the result of the eight MessageBox.Show method calls in the program text. Dialog box [6] only is shown when you specify certain options on the previous three dialogs. It tests the DialogResult enumeration.
DialogResult
In Windows Forms, DialogResult is not an actual class but is a named constant from an enumeration. This means you cannot create a new DialogResult with the new operator. To test the result of DialogResult, first assign your variable to the result of MessageBox.Show. Next, type in "==" and Visual Studio will suggest options from the DialogResult enumeration. You can compare DialogResult like you would compare an integral type such as int.
DialogResult: Windows FormsMore overloads
There are several more overloads of MessageBox.Show that are not shown in this document. They allow you to specify owner windows, which you do not need to do in simple cases. The IWin32Window owner parameter is an interface type, which is indicated by the starting letter I. An interface in the C# language is a contract that can be used to treat object instances more generally. The word interface here does not refer to the user interface.
HelpNavigator parameter. The MessageBox.Show method also has overloads that allow you to specify Help options. In my experience, these options are not used most of the time, so I leave the specifics here up to MSDN.
Obtrusive
When designing programs for the end user, it is usually best to make non-critical errors as unobtrusive as possible. The Microsoft User Experience Guidelines provide many tips on dialog boxes. Next, we look at a quote from that document, and then you can read more on MSDN.
MSDN. Well-written, helpful error messages are crucial to a quality user experience. Poorly written error messages result in low product satisfaction, and are a leading cause of avoidable technical support costs. Unnecessary error messages break users' flow.
msdn.microsoft.comSummary
We saw examples of using MessageBox.Show in your C# programs using Windows Forms. We looked at the actual screenshots of the results of MessageBox.Show method calls, and also tested the DialogResult enumeration. We reviewed many other options of MessageBox.Show, including many parameters to the static method. The MessageBox.Show method is ideal for many simpler Windows Forms program, and is useful for when you want to develop useful programs quickly.
Windows Forms