MessageBox
In C# programs based on Windows Forms, MessageBox
is a dialog. Dialog boxes interrupt users—they force users to respond before further action is taken.
MessageBox.Show
is useful if a warning or error is important. We must choose the number of arguments to pass to MessageBox.Show
—we can use just 1, but often more are needed.
Here are examples of MessageBox.Show
with 1 to 7 arguments. You can match up the images to the code. For more complex dialogs, we need more arguments.
MessageBox.Show
calls into different implementations of the function based on the parameter lists.using System.Windows.Forms; namespace WindowsFormsApp5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Use 1 argument. MessageBox.Show("Dot Net Perls is awesome."); // Use 2 arguments. MessageBox.Show("Dot Net Perls is awesome.", "Important Message"); // Use 3 arguments. DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?", "Important Question", MessageBoxButtons.YesNo); // Use 4 arguments. DialogResult result2 = MessageBox.Show("Is Dot Net Perls awesome?", "Important Query", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); // Use 5 arguments. DialogResult result3 = MessageBox.Show("Is this website awesome?", "The Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); // Test DialogResult. if (result1 == DialogResult.Yes && result2 == DialogResult.Yes && result3 == DialogResult.No) { MessageBox.Show("You answered yes, yes and no."); } // Use 7 arguments. MessageBox.Show("Dot Net Perls is the best.", "Critical Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign, true); } } }
static
The MessageBox.Show
method is a static
method. This means you do not need to create a new MessageBox()
anywhere in your code.
MessageBox
" and press the period, and then select the "Show" entry.MessageBox.Show
method is used in the Form1
constructor, but in your code, you should call it when a dialog is needed.MessageBoxButtons
", type in "MessageBoxButtons
" and press period to see all the options.using System.Windows.Forms; namespace WindowsFormsApp5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Type this part. MessageBox.Show( //... } } }
The order of the parameters in the MessageBox.Show
method calls is important. The compiler applies overload resolution to call the best method in the method group.
DialogResult
This is an enum
. This means you cannot create a new DialogResult
with the "new" operator. First assign your variable to the result of MessageBox.Show
.
DialogResult
enumeration.DialogResult
like you would compare an integral type such as int
. You can even use it in a switch
.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.
IWin32Window
owner parameter is an interface
type. Interfaces treat object instances in a more general way.HelpNavigator
parameterThe MessageBox.Show
method also has overloads that allow you to specify Help options. In my experience, these options are not usually needed.
MessageBox.Show
is an effective approach to dialog boxes in Windows Forms. We looked at screenshots of the results of the MessageBox.Show
method.