首页  >  文章  >  后端开发  >  C# 消息框

C# 消息框

WBOY
WBOY原创
2024-09-03 15:27:42790浏览

在当今的应用程序中,总是需要向用户显示一条消息作为信息或确认的标记,以便用户了解他所执行的操作的状态。该消息可以是任何内容,从“支付成功”到“您想继续吗”等警告类型。这是在 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);
语法 使用 MessageBox.Show(字符串) 它将仅显示带有传递的字符串的消息框。还有一个“确定”按钮可以关闭对话框。
示例: MessageBox.Show(字符串, 字符串) 它将仅显示带有作为第一个参数传递的字符串的消息框。第二个参数是消息框的标题。还有一个“确定”按钮可以关闭对话框。
示例: MessageBox.Show(字符串,字符串,MessageBoxButtons) 它将显示消息框,其中包含提供的文本、标题以及要在消息框中显示的相应按钮。
例如,下面将显示“是”和“否”按钮。 显示(字符串、字符串、MessageBoxButtons、MessageBoxIcon) 它将显示消息框,其中包含提供的文本、标题以及要在消息框中显示的相应按钮。它还会显示在文本之前指定的图标。
例如,下面将显示“是”和“否”按钮,消息前面带有问号。 显示(字符串,字符串,MessageBoxButtons,MessageBoxIcon,MessageBoxDefaulButton) 它将显示消息框,其中包含提供的文本、标题以及要在消息框中显示的相应按钮。它还会在文本之前显示指定的图标。最后一个参数表示加载时必须默认选择哪个按钮。
例如,下面将显示“是”和“否”按钮,消息前面带有问号。 显示(字符串、字符串、MessageBoxButtons、MessageBoxIcon、MessageBoxDefaulButton、MessageBoxOptions) 它将显示消息框,其中包含提供的文本、标题以及要在消息框中显示的相应按钮。它还会在文本之前显示指定的图标。最后一个参数表示加载时默认必须选择哪个按钮,并且消息框的内容将右对齐。
例如,下面将显示“是”和“否”按钮,消息前面带有问号。 表>

Types of MessageBox Buttons

The following are the types of Buttons that are available in the MessageBox.Show() method. They are

  • OK: It is defined as MessageBoxButtons.OK
  • OK and Cancel: It is defined as MessageBoxButtons.OkCancel.
  • Abort Retry and Ignore: It is defined as MessageBoxButtons.AbortRetryIgnore.
  • Yes No and Cancel: It is defined as MessageBoxButtons.YesNoCancel.
  • Yes and No: It is defined as MessageBoxButtons.YesNo.
  • Retry and Cancel: It is defined as MessageBoxButtons.RetryCancel.

Types of MessageBox Icons

The following are the types of MessageBox icons method are:

  • None: No icons are displayed in the Message box.
  • Hand: A hand icon is displayed. It is defined as MessageBoxIcon.Hand.
  • Question: A question mark is displayed. It is defined as MessageBoxIcon.Question.
  • Exclamation: An exclamation mark is displayed. It is defined as MessageBoxIcon.Exclamation.
  • Asterisk: An asterisk symbol is displayed. It is defined as MessageBoxIcon.Asterisk.
  • Stop: A stop icon is displayed. It is defined as MessageBoxIcon.Stop.
  • Error: An error icon is displayed. It is defined as MessageBoxIcon.Error.
  • Warning: A warning icon is displayed. It is defined as MessageBoxIcon.Warning.
  • Information: An info symbol is displayed. It is defined as MessageBoxIcon.Information.

Types of MessageBox Options

The following are the various Message Box options that are available.

  • ServiceNotification: It is defined as MessageBoxOptions.ServiceNotification. This is used to display the message box on the current desktop which is active. The message box is displayed even when no user is logged on to the desktop.
  • DefaultDesktopOnly: It is defined as MessageBoxOptions.DefaultDesktopOnly. This also displays on the currently active desktop. The difference between this and service notification is that here the message is displayed on the interactive window.
  • RightAlign: It is defined as MessageBoxOptions.RightAlign. This is used to format the message in right alignment.
  • RtlReading: It is defined as MessageBoxOptions.RtlReading. This denotes that message is displayed from right to left order.

Example of C# MessageBox

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:

C# 消息框

C# 消息框

Conclusion – C# MessageBox

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Checkbox in C#下一篇:Collections in C#