Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday 22 March 2014

How To Create Message Box In .Net

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.


How To Create C#.Net Message Box - Dialog Box
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:


Simple Message Box In C sharp dot net







 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:

Message Box With Title Text ~ C sharp dot net







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:


Message Box With Buttons ~ C sharp Dot Net







MessageBoxButtons enumeration contains following options:
  • MessageBoxButtons.AbortRetryIgnore
  • MessageBoxButtons.OK
  • MessageBoxButtons.OKCancel
  • MessageBoxButtons.RetryCancel
  • MessageBoxButtons.YesNo
  • MessageBoxButtons.YesNoCancel
If you want to get result of what button user clicked then you have to use following code:

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");
}


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:

Message Box with Icons ~ C Sharp Dot Net ~ dialog box with icon








MessageBoxIcon enumeration contains following options:
  • MessageBoxIcon.Asterisk
  • MessageBoxIcon.Error
  • MessageBoxIcon.Exclamation
  • MessageBoxIcon.Hand
  • MessageBoxIcon.Information
  • MessageBoxIcon.None
  • MessageBoxIcon.Question
  • MessageBoxIcon.Stop
  • MessageBoxIcon.Warning
As your requirement, you can use any above options with your message box.

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:

Dialog Box - Message Box with default button settings ~ C sharp Dot Net








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
In above code, we use RightAlign option and that message box looks like:

Message Box - Dialog Box with other options ~ C sharp Dot Net









If you find any problem then comment below.