在当今的应用程序中,总是需要向用户显示一条消息作为信息或确认的标记,以便用户了解他所执行的操作的状态。该消息可以是任何内容,从“支付成功”到“您想继续吗”等警告类型。这是在 C# 中借助 Message Box 实现的。消息框可以被视为用户和应用程序之间的接口。它只不过是一个带有文本、图像或符号的窗口,用于指导或向用户传达某些内容。在执行适当的操作并关闭消息框之前,不允许执行其他操作。
语法:
消息框是“Systems.Windows.Forms”命名空间中的一个类,它可用的程序集是“System.Windows.Forms.dll”。该类中可用的 show 方法用于显示消息以及操作按钮。操作按钮可以是从“是”到“否”、“确定”到“取消”的任何内容。
示例:
以下代码将创建一个仅带有“确定”按钮的简单消息框。
string msg = "Test"; MessageBox.Show(msg);
以下是show方法的类型:
Syntax | 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("Test") |
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( “Message”, ”Title”). |
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. MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo); |
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. MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question); |
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. MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); |
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. MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MesageBoxOptions.RightAlign, true); |
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.
以上是C# 消息框的详细内容。更多信息请关注PHP中文网其他相关文章!