Message Box in Windows Forms using C#

MessageBox control in Windows Forms is used to display a message with the given text. You can also use MessageBox control to add additional options such as a caption, an icon, or help buttons.
 
In this article, I will discuss how to use the MessageBox class to display messages in Windows Forms and C#.
 
Get Started
 
Create a Windows Forms application using Visual Studio. 
 
MessageBox class has an overloaded static Show method that is used to display a message.
 
Simple MessageBox

The simplest form of a MessageBox is a dialog with a text and OK button. When you click OK button, the box disappears.
 
The following code snippet creates a simple Message Box.  
  1. string message = "Simple MessageBox";    
  2. MessageBox.Show(message);    
MessageBox with Title
 
The following code snippet creates a simple MessageBox with a title.
  1. string message = "Simple MessageBox";  
  2. string title = "Title";  
  3. MessageBox.Show(message, title);  
 MsgBoxImg2.jpg

MessageBox with Buttons

A MessageBox can have different button combination such as YesNo and OKCancel. The MessageBoxButtons enumeration represents the buttons to be displayed on a MessageBox and has following values.
  • OK
  • OKCancel
  • AbortRetryIgnore
  • YesNoCancel
  • YesNo
  • RetryCancel
The following code snippet creates a MessageBox with a title and Yes and No buttons. This is a typical MessageBox you may call when you want to close an application. If Yes button is clicked, the application will be closed. The Show method returns a DialogResult enumeration.
  1. string message = "Do you want to close this window?";
  2. string title = "Close Window";
  3. MessageBoxButtons buttons = MessageBoxButtons.YesNo;
  4. DialogResult result = MessageBox.Show(message, title, buttons);
  5. if (result == DialogResult.Yes)
  6. {
  7. this.Close();
  8. }
  9. else
  10. {
  11. // Do something
  12. }

MsgBoxImg3.jpg

MessageBox with Icon
 
A MessageBox can display an icon on the dialog. A MessageBoxIcons enumeration represents an icon to be displayed on a MessageBox and has following values.
  • None
  • Hand
  • Question
  • Exclamation
  • Asterisk
  • Stop
  • Error
  • Warning
  • Information
The following code snippet creates a MessageBox with a title, buttons, and an icon.
  1. string message = "Do you want to abort this operation?";
  2. string title = "Close Window";
  3. MessageBoxButtons buttons = MessageBoxButtons.AbortRetryIgnore;
  4. DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning);
  5. if (result == DialogResult.Abort)
  6. {
  7. this.Close();
  8. }
  9. else if (result == DialogResult.Retry)
  10. {
  11. // Do nothing
  12. }
  13. else
  14. {
  15. // Do something
  16. }

MsgBoxImg4.jpg

MessageBox with Default Button
 
We can also set the default button on a MessageBox. By default, the first button is the default button. The MessageBoxDefaultButton enumeration is used for this purpose and it has the following three values.
  • Button1
  • Button2
  • Button3
The following code snippet creates a MessageBox with a title, buttons, and an icon and sets second button as a default button.
  1. string message = "Do you want to abort this operation?";
  2. string title = "Close Window";
  3. MessageBoxButtons buttons = MessageBoxButtons.AbortRetryIgnore;
  4. DialogResult result = MessageBox.Show(message, title, buttons,
  5. MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
  6. if (result == DialogResult.Abort)
  7. {
  8. this.Close();
  9. }
  10. else if (result == DialogResult.Retry)
  11. {
  12. // Do nothing
  13. }
  14. else
  15. {
  16. // Do something
  17. }

MsgBoxImg5.jpg

MessageBox with Message Options

MessageBoxOptions enumeration represents various options and has following values.
  • ServiceNotification
  • DefaultDesktopOnly
  • RightAlign
  • RtlReading
The following code snippet creates a MessageBox with various options.
  1. DialogResult result = MessageBox.Show(message, title, buttons,
  2. MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2,
  3. MessageBoxOptions.RightAlign|MessageBoxOptions.RtlReading);

MsgBoxImg6.jpg

MessageBox with Help Button
 
A MessageBox can have an extra button called Help button. This is useful when we need to display a help file. The following code snippet creates a MessageBox with a Help button.
  1. DialogResult result = MessageBox.Show(message, title, buttons,
  2. MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2,
  3. MessageBoxOptions.RightAlign, true );
MsgBoxImg7.jpg

We can also specify a help file when the Help button is clicked. The following code snippet references a help file.
  1. DialogResult result = MessageBox.Show(message, title,
  2. buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, "helpfile.chm");
Summary
 
In this article, we discussed discuss how to create and use a MessageBox in a Windows Forms application.