In today’s applications, it is always required that a message is displayed to the user as a token of information or confirmation so that the user is aware of the status of the operation he performed. The message can be anything ranging from “The payment is successful” or a warning type like “Do you want to continue” etc. This is achieved in C# with the help of Message Box. A message box can be considered as an interface between the user and the application. It is nothing but a window that has text, images, or symbols to guide or convey something to the user. Until appropriate action is performed, and the message box is closed, it will not allow other actions to be performed.
Syntax:
Message Box is a class in the “Systems.Windows.Forms” Namespace and the assembly it is available is “System.Windows.Forms.dll”.The show method available in the class is used to display the message along with action buttons. The action buttons can be anything ranging from Yes to No, Ok to Cancel.
Example:
The following code will create a simple Message Box only with the OK button.
string msg = "Test"; MessageBox.Show(msg);
Following are the types of show method:
|
Use | ||||||||||||||
MessageBox.Show(String) | It will display only the message box with the string that is passed. An ok button is also present to close the dialog. Example: |
||||||||||||||
MessageBox.Show( String, String) | It will display only the message box with the string that is passed as first parameter. The second parameter is the title of the Message Box. An ok button is also present to close the dialog. Example: |
||||||||||||||
MessageBox.Show( String,String, MessageBoxButtons) | It will display the message box with the supplied text, title and the corresponding buttons to be displayed on the Message Box. For eg the below will display Yes and No buttons. |
||||||||||||||
Show(String, String, MessageBoxButtons, MessageBoxIcon) | It will display the message box with the supplied text, title and the corresponding buttons to be displayed on the Message Box. It will also display the icon that is specified before the text. For eg the below will display Yes and No buttons with a question mark in front of message. |
||||||||||||||
Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton) | It will display the message box with the supplied text, title and the corresponding buttons to be displayed on the Message Box. It will also display the icon that is specified before the text. The last parameter denotes which button must be selected by default on load. For eg the below will display Yes and No buttons with a question mark in front of message. |
||||||||||||||
Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton, MessageBoxOptions) | It will display the message box with the supplied text, title, and the corresponding buttons to be displayed on the Message Box. It will also display the icon that is specified before the text. The last parameter denotes which button must be selected by default on load and the contents of the messagebox will be right-aligned. For eg the below will display Yes and No buttons with a question mark in front of message. |
The following are the types of Buttons that are available in the MessageBox.Show() method. They are
The following are the types of MessageBox icons method are:
The following are the various Message Box options that are available.
Following are the examples of c# message box are:
Input:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace test { public partial class testform : Form { public testform() { InitializeComponent(); } private void testform_Load(object sender, EventArgs e) { MessageBox.Show("Demo of MsgBox"); MessageBox.Show("Demo of MsgBox", "Title"); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNo); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.OkCancel); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.RetryCancel); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.OK); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.AbortRetryIgnore); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Hand); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Asterisk); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Stop); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Error); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Information);MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2); MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1); } } }
Output:
Thus, the article covered in detail about the Message box class in c# in detail. It explained about various message box show methods that are available, the various parameters of each method, and demonstrated that with an example. The article also covered in detail about various message box options, message box buttons, and message box icons in detail along with their use. To learn more in detail it is advisable to write sample programs and practice them.
The above is the detailed content of C# MessageBox. For more information, please follow other related articles on the PHP Chinese website!