MessageBox.Show ExamplesUse the MessageBox.Show method in Windows Forms to display a dialog box. Change the buttons and text.
This page was last reviewed on Dec 27, 2021.
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.
Shows a messageboxShows a messagebox
Method notes. 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.
Some examples. 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.
Tip We control the message body text, the buttons, the icons, the message box title, and the default button focus.
Info MessageBox.Show calls into different implementations of the function based on the parameter lists.
Shows a messagebox
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); } } }
Notes, static. The MessageBox.Show method is a static method. This means you do not need to create a new MessageBox() anywhere in your code.
Detail You can type "MessageBox" and press the period, and then select the "Show" entry.
Here The MessageBox.Show method is used in the Form1 constructor, but in your code, you should call it when a dialog is needed.
Also For a parameter such as "MessageBoxButtons", type in "MessageBoxButtons" and press period to see all the options.
Shows a messagebox
using System.Windows.Forms; namespace WindowsFormsApp5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Type this part. MessageBox.Show( //... } } }
Parameter order. 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.
Next Type in "==" and Visual Studio will suggest options from the DialogResult enumeration.
Tip You can compare DialogResult like you would compare an integral type such as int. You can even use it in a switch.
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.
Detail The IWin32Window owner parameter is an interface type. Interfaces treat object instances in a more general way.
HelpNavigator parameter. The MessageBox.Show method also has overloads that allow you to specify Help options. In my experience, these options are not usually needed.
A summary. MessageBox.Show is an effective approach to dialog boxes in Windows Forms. We looked at screenshots of the results of the MessageBox.Show method.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 27, 2021 (edit link).
© 2007-2024 Sam Allen.