MessageBox 类

定义

显示消息窗口(也称为对话框),向用户显示消息。 这是一个模式窗口,可阻止应用程序中的其他操作,直到用户将其关闭。 MessageBox 可包含通知并指示用户的文本、按钮和符号。

C#
public class MessageBox
继承
MessageBox

示例

下面的代码示例演示如何使用 a MessageBox 来通知用户缺少的 TextBox条目。 此示例要求从具有 TextBox 命名 ServerName 的现有窗体调用该方法。

C#
private void validateUserEntry()
{
    // Checks the value of the text.
    if(serverName.Text.Length == 0)
    {
        // Initializes the variables to pass to the MessageBox.Show method.
        string message = "You did not enter a server name. Cancel this operation?";
     string caption = "Error Detected in Input";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.
        result = MessageBox.Show(message, caption, buttons);
        if (result == System.Windows.Forms.DialogResult.Yes)
        {
            // Closes the parent form.
            this.Close();
        }
    }
}

下面的代码示例演示如何向用户询问“是”或“否”,并根据响应做出决定。

C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    const string message =
        "Are you sure that you would like to close the form?";
    const string caption = "Form Closing";
    var result = MessageBox.Show(message, caption,
                                 MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question);

    // If the no button was pressed ...
    if (result == DialogResult.No)
    {
        // cancel the closure of the form.
        e.Cancel = true;
    }
}

注解

不能创建新类实例 MessageBox 。 若要显示消息框,请调用 static 方法 MessageBox.Show。 消息框中显示的标题、消息、按钮和图标由传递给此方法的参数确定。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Show(IWin32Window, String)

在指定对象的前面显示具有指定文本的消息框。

Show(IWin32Window, String, String)

在指定对象的前面显示具有指定文本和标题的消息框。

Show(IWin32Window, String, String, MessageBoxButtons)

在指定对象的前面显示具有指定文本、标题和按钮的消息框。

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon)

在指定对象的前面显示具有指定文本、标题、按钮和图标的消息框。

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

在指定对象的前面显示具有指定文本、标题、按钮、图标和默认按钮的消息框。

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)

在指定对象的前面显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String)

使用指定的帮助文件显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator)

使用指定的帮助文件和 HelpNavigator 显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object)

使用指定的帮助文件、HelpNavigator 和帮助主题显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String)

使用指定的帮助文件和帮助关键字显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(String)

显示具有指定文本的消息框。

Show(String, String)

显示具有指定文本和标题的消息框。

Show(String, String, MessageBoxButtons)

显示具有指定文本、标题和按钮的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon)

显示具有指定文本、标题、按钮和图标的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

显示具有指定文本、标题、按钮、图标和默认按钮的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)

显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, Boolean)

显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String)

使用指定的帮助文件显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator)

使用指定的帮助文件和 HelpNavigator 显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object)

使用指定的帮助文件、HelpNavigator 和帮助主题显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String)

使用指定的帮助文件和帮助关键字显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅