Message Box is used to inform something to the user. Message box interrupts user and user must have to answer message box to process further. Most of time this type of message box you find in error message, warning message, etc.
In this tutorial, I am explaining about C#.Net message box.
To create message box in C#.Net, you have to include 'System.Windows.Forms' namespace.
Example:
The name of class used to create message box is 'MessageBox' and contains static method named 'Show()'.
Static method does not need to call using object, it is called directly using class name.
Message box sometimes called dialog box or popup box.
Following are the examples of message box and it is written in Form1_Load event of window form.
This message box contains just message and no title text displayed.
Simple message box looks like:
This message box contains text message and title text.
This message box looks like:
This message box contains text message, title text, Yes and No buttons.
This message box looks like:
MessageBoxButtons enumeration contains following options:
Using MessageBoxIcon enumeration, you can change icon of message box.
This message box looks like:
MessageBoxIcon enumeration contains following options:
Using MessageBoxDefaultButton enumeration, you can set default button. If you set default Button2 then button - 2 is selected.
This message box looks like:
MessageBoxDefaultButton enumeration cotains following options:
Using MessageBoxOptions enumeration, you can set following options:
In this tutorial, I am explaining about C#.Net message box.
To create message box in C#.Net, you have to include 'System.Windows.Forms' namespace.
C#.Net Message Box |
Example:
using System.Windows.Forms;
The name of class used to create message box is 'MessageBox' and contains static method named 'Show()'.
Static method does not need to call using object, it is called directly using class name.
Message box sometimes called dialog box or popup box.
Following are the examples of message box and it is written in Form1_Load event of window form.
1. Simple Message Box
MessageBox.Show("Simple Message Box");
This message box contains just message and no title text displayed.
Simple message box looks like:
2. Message Box With Message and Title Text
MessageBox.Show("All Code Tips ~ Programming Blog", "Title: All Code Tips");
This message box contains text message and title text.
This message box looks like:
3. Message Box With Different Buttons
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo);
This message box contains text message, title text, Yes and No buttons.
This message box looks like:
MessageBoxButtons enumeration contains following options:
- MessageBoxButtons.AbortRetryIgnore
- MessageBoxButtons.OK
- MessageBoxButtons.OKCancel
- MessageBoxButtons.RetryCancel
- MessageBoxButtons.YesNo
- MessageBoxButtons.YesNoCancel
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo);
if(res == DialogResult.Yes)
{
MessageBox.Show("You click on Yes button");
}
else
{
MessageBox.Show("You click on No button");
}
if(res == DialogResult.Yes)
{
MessageBox.Show("You click on Yes button");
}
else
{
MessageBox.Show("You click on No button");
}
4. Message Box With Icons
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
Using MessageBoxIcon enumeration, you can change icon of message box.
This message box looks like:
MessageBoxIcon enumeration contains following options:
- MessageBoxIcon.Asterisk
- MessageBoxIcon.Error
- MessageBoxIcon.Exclamation
- MessageBoxIcon.Hand
- MessageBoxIcon.Information
- MessageBoxIcon.None
- MessageBoxIcon.Question
- MessageBoxIcon.Stop
- MessageBoxIcon.Warning
5. Default Button Settings
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
Using MessageBoxDefaultButton enumeration, you can set default button. If you set default Button2 then button - 2 is selected.
This message box looks like:
MessageBoxDefaultButton enumeration cotains following options:
- MessageBoxDefaultButton.Button1
- MessageBoxDefaultButton.Button2
- MessageBoxDefaultButton.Button3
6. Setting Message Box Options
DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign);
Using MessageBoxOptions enumeration, you can set following options:
- MessageBoxOptions.DefaultDesktopOnly
- MessageBoxOptions.RightAlign
- MessageBoxOptions.RtlReading
- MessageBoxOptions.ServiceNotification
If you find any problem then comment below.