MessageBox.Show 方法

定义

显示消息框。

重载

名称 说明
Show(String)

显示包含指定文本的消息框。

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

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

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

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

Show(IWin32Window, 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, String)

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

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

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

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

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

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

显示一个消息框,其中包含指定的文本、标题、按钮、图标、默认按钮、选项和帮助按钮。

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

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

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

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

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

在指定对象前面显示一个消息框,并显示指定的文本、标题、按钮和图标。

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

显示一个消息框,其中包含指定的文本、标题、按钮、图标和默认按钮。

Show(IWin32Window, String, String, MessageBoxButtons)

在指定对象前面显示一个消息框,并带有指定的文本、标题和按钮。

Show(String, String, MessageBoxButtons, MessageBoxIcon)

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

Show(IWin32Window, String, String)

在指定对象前面显示一个消息框,并带有指定的文本和标题。

Show(String, String, MessageBoxButtons)

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

Show(IWin32Window, String)

在指定对象前面显示一个消息框,并显示具有指定文本。

Show(String, String)

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

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

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

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

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

Show(String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

显示包含指定文本的消息框。

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text);
public static System.Windows.Forms.DialogResult Show(string text);
public static System.Windows.Forms.DialogResult Show(string? text);
static member Show : string -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String) As DialogResult

参数

text
String

要显示在消息框中的文本。

返回

其中一个 DialogResult 值。

示例

下面的代码示例显示一个简单的消息框。

private void DisplayMessageBoxText()
{
         MessageBox.Show("Hello, world.");
}
Private Sub DisplayMessageBoxText()

    MessageBox.Show("Hello, world.")
End Sub

注解

默认情况下,消息框显示 “确定 ”按钮。 消息框不包含标题。

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath, System::String ^ keyword);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string * string -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String, keyword As String) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

keyword
String

当用户单击“帮助”按钮时显示的帮助关键字。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示主窗口父级的消息框。 消息框显示“帮助”按钮。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件,并显示关键字标识 mspaint.chm::/paint_brush.htm 的主题。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file, 
// and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
// associated topic.
System::Windows::Forms::DialogResult r8 = MessageBox::Show( this, "Message with Help file and keyword.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm", "mspaint.chm::/paint_brush.htm" );
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file, 
// and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
// associated topic.
DialogResult r8 = MessageBox.Show (this, "Message with Help file and keyword.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 0, 
                                   "mspaint.chm", 
                                   "mspaint.chm::/paint_brush.htm");
' Display message box parented to the main form. 
' The Help button opens the Mspaint.chm Help file, 
' and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
' associated topic.
Dim r8 As DialogResult = MessageBox.Show(Me, "Message with Help file and keyword.", _
                                   "Help Caption", MessageBoxButtons.OK, _
                                   MessageBoxIcon.Question, _
                                   MessageBoxDefaultButton.Button1, 0, _
                                   "mspaint.chm", _
                                   "mspaint.chm::/paint_brush.htm")

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。 可以使用 owner 参数来指定实现 IWin32Window 接口的特定对象,该对象将充当对话框的顶级所有者。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件,并显示由该参数标识的 keyword Help 关键字主题。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath, System::Windows::Forms::HelpNavigator navigator, System::Object ^ param);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object param);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object? param);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string * System.Windows.Forms.HelpNavigator * obj -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String, navigator As HelpNavigator, param As Object) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

navigator
HelpNavigator

其中一个 HelpNavigator 值。

param
Object

当用户单击“帮助”按钮时要显示的帮助主题的数字 ID。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示包含“帮助”按钮的消息框。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件,并显示由关键字标识的 ovals 帮助索引选项卡和主题。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display a message box. The Help button opens the Mspaint.chm Help file, 
// shows index with the "ovals" keyword selected, and displays the
// associated topic.
System::Windows::Forms::DialogResult r5 = MessageBox::Show( "Message with Help file and Help navigator with additional parameter.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm", HelpNavigator::KeywordIndex, "ovals" );
// Display a message box. The Help button opens the Mspaint.chm Help file, 
// shows index with the "ovals" keyword selected, and displays the
// associated topic.
DialogResult r5 = MessageBox.Show ("Message with Help file and Help navigator with additional parameter.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.KeywordIndex, "ovals");
' Display a message box. The Help button opens the Mspaint.chm Help file, 
' shows index with the "ovals" keyword selected, and displays the
' associated topic.
Dim r5 As DialogResult = MessageBox.Show("Message with Help file and Help navigator with additional parameter.", _
                                   "Help Caption", MessageBoxButtons.OK, _
                                   MessageBoxIcon.Question, _
                                   MessageBoxDefaultButton.Button1, _
                                   0, "mspaint.chm", _
                                   HelpNavigator.KeywordIndex, "ovals")

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件,并显示由该参数标识的 navigator 帮助内容。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

编译的帮助文件提供页面中的目录、索引、搜索和关键字链接。 可使用以下值:navigator、、TableOfContentsFindIndexTopic

可用于 param 进一步优化 Topic 命令。 如果参数中指定的navigator值为,则TableOfContents此值应为IndexFindnull此值。 如果参数 navigator 引用 Topic,此值应引用包含要显示的主题的数值的对象。

参数 helpFilePath 可以是 C:\path\sample.chm 或 /folder/file.htm。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示主窗体的父级消息框。 消息框显示“帮助”按钮。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display a message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file.
System::Windows::Forms::DialogResult r2 = MessageBox::Show( this, "Message with Help file.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm" );
// Display a message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file.
DialogResult r2 = MessageBox.Show (this, "Message with Help file.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, 
                                   "mspaint.chm");
' Display a message box parented to the main form. 
' The Help button opens the Mspaint.chm Help file.
Dim r2 As DialogResult = MessageBox.Show(Me, "Message with Help file.", _
                                   "Help Caption", MessageBoxButtons.OK, _
                                   MessageBoxIcon.Question, _
                                   MessageBoxDefaultButton.Button1, _
                                   0, _
                                   "mspaint.chm")

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。 可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

参数 helpFilePath 可以是 C:\path\sample.chm 或 /folder/file.htm。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath, System::Windows::Forms::HelpNavigator navigator);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string * System.Windows.Forms.HelpNavigator -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String, navigator As HelpNavigator) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

navigator
HelpNavigator

其中一个 HelpNavigator 值。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示包含“帮助”按钮的消息框。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件,并显示“帮助索引”选项卡。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display a message box. The Help button opens 
// the Mspaint.chm Help file and shows the Help contents 
// on the Index tab.
System::Windows::Forms::DialogResult r3 = MessageBox::Show( "Message with Help file and Help navigator.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm", HelpNavigator::Index );
// Display a message box. The Help button opens 
// the Mspaint.chm Help file and shows the Help contents 
// on the Index tab.
DialogResult r3 = MessageBox.Show ("Message with Help file and Help navigator.", 
                                   "Help Caption", MessageBoxButtons.OK,
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.Index);
' Display a message box. The Help button opens 
' the Mspaint.chm Help file and shows the Help contents 
' on the Index tab.
Dim r3 As DialogResult = MessageBox.Show("Message with Help file and Help navigator.", _
                                   "Help Caption", MessageBoxButtons.OK, _
                                   MessageBoxIcon.Question, _
                                   MessageBoxDefaultButton.Button1, _
                                   0, "mspaint.chm", _
                                   HelpNavigator.Index)

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件,并显示由该参数标识的 navigator 帮助内容。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

编译的帮助文件在页面中提供目录、索引、搜索和关键字链接。 可使用以下值:navigator、、TableOfContentsFindIndexTopic

参数 helpFilePath 可以是 C:\path\sample.chm 或 /folder/file.htm。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath, System::String ^ keyword);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string * string -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String, keyword As String) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

keyword
String

当用户单击“帮助”按钮时显示的帮助关键字。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示包含“帮助”按钮的消息框。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件,并显示关键字标识 mspaint.chm::/paint_brush.htm 的主题。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display a message box. The Help button opens the Mspaint.chm Help file, 
// and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
// associated topic.
System::Windows::Forms::DialogResult r7 = MessageBox::Show( "Message with Help file and keyword.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm", "mspaint.chm::/paint_brush.htm" );
// Display a message box. The Help button opens the Mspaint.chm Help file, 
// and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
// associated topic.
DialogResult r7 = MessageBox.Show ("Message with Help file and keyword.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 0, 
                                   "mspaint.chm", 
                                   "mspaint.chm::/paint_brush.htm");
' Display a message box. The Help button opens the Mspaint.chm Help file, 
' and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
' associated topic.
Dim r7 As DialogResult = MessageBox.Show("Message with Help file and keyword.", _
                                   "Help Caption", MessageBoxButtons.OK, _
                                   MessageBoxIcon.Question, _
                                   MessageBoxDefaultButton.Button1, 0, _
                                   "mspaint.chm", _
                                   "mspaint.chm::/paint_brush.htm")

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件,并显示由该参数标识的 keyword Help 关键字主题。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

options 指定 DefaultDesktopOnlyServiceNotification 指定参数中的 owner 值。 仅当调用不采用参数的此方法版本时,才应使用这两个选项 owner

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示 MessageBox 此重载 Show支持的选项。 验证字符串变量是否 ServerName为空后,该示例会显示一个 MessageBox,为用户提供取消操作的选项。 Show如果方法的返回值计算结果为Yes,则显示MessageBox该方法的窗体已关闭。

private:
   void validateUserEntry2()
   {
      // 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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;
         
         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, MessageBoxOptions::RightAlign );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }

private void validateUserEntry2()
{

    // 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 = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons,
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
            MessageBoxOptions.RightAlign);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}
Private Sub ValidateUserEntry2()


    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "No Server Name Specified"
        Dim Buttons As Integer = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays a MessageBox using the Question icon and specifying the No button as the default.

        Result = MessageBox.Show(Me, Message, Caption, MessageBoxButtons.YesNo, _
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)


        ' Gets the result of the MessageBox display.

        If Result = System.Windows.Forms.DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If

End Sub

注解

可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。 消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

消息框中最多可以有三个按钮。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示包含“帮助”按钮的消息框。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display a message box with a help button. 
// The Help button opens the Mspaint.chm Help file.
System::Windows::Forms::DialogResult r1 = MessageBox::Show( "Message with Help file.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm" );
// Display a message box with a help button. 
// The Help button opens the Mspaint.chm Help file.
DialogResult r1 = MessageBox.Show ("Message with Help file.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, 
                                   "mspaint.chm");
' Display a message box with a help button. 
' The Help button opens the Mspaint.chm Help file.
Dim r1 As DialogResult = MessageBox.Show("Message with Help file.", _
                                   "Help Caption", MessageBoxButtons.OK, _
                                   MessageBoxIcon.Question, _
                                   MessageBoxDefaultButton.Button1, _
                                   0, _
                                   "mspaint.chm")

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

参数 helpFilePath 可以是 C:\path\sample.chm 或 /folder/file.htm。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

显示一个消息框,其中包含指定的文本、标题、按钮、图标、默认按钮、选项和帮助按钮。

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, bool displayHelpButton);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, bool displayHelpButton);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, bool displayHelpButton);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * bool -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, displayHelpButton As Boolean) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

displayHelpButton
Boolean

true 显示“帮助”按钮;否则,为 false. 默认值为 false

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示包含帮助按钮的消息框、处理 Control.HelpRequested 事件以及显示自定义帮助窗口以响应事件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display a message box with a Help button. Show a custom Help window
// by handling the HelpRequested event.
System::Windows::Forms::DialogResult AlertMessageWithCustomHelpWindow()
{
   
   // Handle the HelpRequested event for the following message.
   this->HelpRequested += gcnew System::Windows::Forms::HelpEventHandler( this, &Form1::Form1_HelpRequested );
   this->Tag = "Message with Help button.";
   
   // Show a message box with OK and Help buttons.
   System::Windows::Forms::DialogResult r = MessageBox::Show( "Message with Help button.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, true );
   
   // Remove the HelpRequested event handler to keep the event
   // from being handled for other message boxes.
   this->HelpRequested -= gcnew System::Windows::Forms::HelpEventHandler( this, &Form1::Form1_HelpRequested );
   
   // Return the dialog box result.
   return r;
}

void Form1_HelpRequested( System::Object^ sender, System::Windows::Forms::HelpEventArgs^ hlpevent )
{
   
   // Create a custom Help window in response to the HelpRequested event.
   Form^ helpForm = gcnew Form;
   
   // Set up the form position, size, and title caption.
   helpForm->StartPosition = FormStartPosition::Manual;
   helpForm->Size = System::Drawing::Size( 200, 400 );
   helpForm->DesktopLocation = Point(this->DesktopBounds.X + this->Size.Width,this->DesktopBounds.Top);
   helpForm->Text = "Help Form";
   
   // Create a label to contain the Help text.
   Label^ helpLabel = gcnew Label;
   
   // Add the label to the form and set its text.
   helpForm->Controls->Add( helpLabel );
   helpLabel->Dock = DockStyle::Fill;
   
   // Use the sender parameter to identify the context of the Help request.
   // The parameter must be cast to the Control type to get the Tag property.
   Control^ senderControl = dynamic_cast<Control^>(sender);
   helpLabel->Text = String::Format( "Help information shown in response to user action on the '{0}' message.", dynamic_cast<String^>(senderControl->Tag) );
   
   // Set the Help form to be owned by the main form. This helps
   // to ensure that the Help form is disposed of.
   this->AddOwnedForm( helpForm );
   
   // Show the custom Help window.
   helpForm->Show();
   
   // Indicate that the HelpRequested event is handled.
   hlpevent->Handled = true;
}
// Display a message box with a Help button. Show a custom Help window
// by handling the HelpRequested event.
private DialogResult AlertMessageWithCustomHelpWindow ()
{
    // Handle the HelpRequested event for the following message.
    this.HelpRequested += new System.Windows.Forms.HelpEventHandler (this.Form1_HelpRequested);

    this.Tag = "Message with Help button.";

    // Show a message box with OK and Help buttons.
    DialogResult r = MessageBox.Show ("Message with Help button.", 
                                      "Help Caption", MessageBoxButtons.OK, 
                                      MessageBoxIcon.Question, 
                                      MessageBoxDefaultButton.Button1, 
                                      0, true);

    // Remove the HelpRequested event handler to keep the event
    // from being handled for other message boxes.
    this.HelpRequested -= new System.Windows.Forms.HelpEventHandler (this.Form1_HelpRequested);

    // Return the dialog box result.
    return r;
}

private void Form1_HelpRequested (System.Object sender, System.Windows.Forms.HelpEventArgs hlpevent)
{
    // Create a custom Help window in response to the HelpRequested event.
    Form helpForm = new Form ();

    // Set up the form position, size, and title caption.
    helpForm.StartPosition = FormStartPosition.Manual;
    helpForm.Size = new Size (200, 400);
    helpForm.DesktopLocation = new Point (this.DesktopBounds.X + 
                                          this.Size.Width, 
                                          this.DesktopBounds.Top);
    helpForm.Text = "Help Form";

    // Create a label to contain the Help text.
    Label helpLabel = new Label ();

    // Add the label to the form and set its text.
    helpForm.Controls.Add (helpLabel);
    helpLabel.Dock = DockStyle.Fill;

    // Use the sender parameter to identify the context of the Help request.
    // The parameter must be cast to the Control type to get the Tag property.
    Control senderControl = sender as Control;

    helpLabel.Text = "Help information shown in response to user action on the '" + 
                      (string)senderControl.Tag + "' message.";

    // Set the Help form to be owned by the main form. This helps
    // to ensure that the Help form is disposed of.
    this.AddOwnedForm (helpForm);

    // Show the custom Help window.
    helpForm.Show ();

    // Indicate that the HelpRequested event is handled.
    hlpevent.Handled = true;
}
' Display a message box with a Help button. Show a custom Help window
' by handling the HelpRequested event.
Private Function AlertMessageWithCustomHelpWindow() As DialogResult

    ' Handle the HelpRequested event for the following message.
    AddHandler Me.HelpRequested, AddressOf Me.Form1_HelpRequested

    Me.Tag = "Message with Help button."

    ' Show a message box with OK and Help buttons.
    Dim r As DialogResult = MessageBox.Show("Message with Help button.", _
                                          "Help Caption", MessageBoxButtons.OK, _
                                          MessageBoxIcon.Question, _
                                          MessageBoxDefaultButton.Button1, _
                                          0, True)

    ' Remove the HelpRequested event handler to keep the event
    ' from being handled for other message boxes.
    RemoveHandler Me.HelpRequested, AddressOf Me.Form1_HelpRequested

    ' Return the dialog box result.
    Return r
End Function

Private Sub Form1_HelpRequested(ByVal sender As System.Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs)

    ' Create a custom Help window in response to the HelpRequested event.
    Dim helpForm As Form = New Form

    ' Set up the form position, size, and title caption.
    With helpForm
        .StartPosition = FormStartPosition.Manual
        .Size = New Size(200, 400)
        .DesktopLocation = New Point(Me.DesktopBounds.X + _
                                     Me.Size.Width, Me.DesktopBounds.Top)
        .Text = "Help Form"
    End With

    ' Create a label to contain the Help text.
    Dim helpLabel As Label = New Label

    ' Add the label to the form and set its text.
    helpForm.Controls.Add(helpLabel)
    helpLabel.Dock = DockStyle.Fill

    ' Use the sender parameter to identify the context of the Help request.
    ' The parameter must be cast to the Control type to get the Tag property.
    Dim senderControl As Control = CType(sender, Control)

    helpLabel.Text = "Help information shown in response to user action on the '" & _
                      CStr(senderControl.Tag) & "' message."

    ' Set the Help form to be owned by the main form. This helps
    ' to ensure that the Help form is disposed of.
    Me.AddOwnedForm(helpForm)

    ' Show the custom Help window.
    helpForm.Show()

    ' Indicate that the HelpRequested event is handled.
    hlpevent.Handled = True
End Sub

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

当用户单击“帮助”按钮时,拥有消息框(或活动窗体)的窗体将收到该 HelpRequested 事件。 HelpRequested处理事件以执行自定义帮助操作。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath, System::Windows::Forms::HelpNavigator navigator);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string * System.Windows.Forms.HelpNavigator -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String, navigator As HelpNavigator) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

navigator
HelpNavigator

其中一个 HelpNavigator 值。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示主窗口父级的消息框。 消息框显示“帮助”按钮。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件,并显示“帮助索引”选项卡。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file
// and shows the Help contents on the Index tab.
System::Windows::Forms::DialogResult r4 = MessageBox::Show( this, "Message with Help file and Help navigator.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm", HelpNavigator::Index );
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file
// and shows the Help contents on the Index tab.
DialogResult r4 = MessageBox.Show (this, 
                                   "Message with Help file and Help navigator.", 
                                   "Help Caption", MessageBoxButtons.OK,
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.Index);
' Display message box parented to the main form. 
' The Help button opens the Mspaint.chm Help file
' and shows the Help contents on the Index tab.
Dim r4 As DialogResult = MessageBox.Show(Me, _
                                      "Message with Help file and Help navigator.", _
                                      "Help Caption", MessageBoxButtons.OK, _
                                      MessageBoxIcon.Question, _
                                      MessageBoxDefaultButton.Button1, _
                                      0, "mspaint.chm", _
                                      HelpNavigator.Index)

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。 可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件,并显示由该参数标识的 navigator 帮助内容。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

编译的帮助文件在页面中提供目录、索引、搜索和关键字链接。 可使用以下值:navigator、、TableOfContentsFindIndexTopic

参数 helpFilePath 可以是 C:\path\sample.chm 或 /folder/file.htm。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

示例

下面的代码示例演示如何显示 MessageBox 此重载 Show支持的选项。 验证字符串变量是否 ServerName为空后,该示例会显示一个 MessageBox,为用户提供取消操作的选项。 Show如果方法的返回值计算结果为Yes,则显示MessageBox该方法的窗体已关闭。

private:
   void validateUserEntry2()
   {
      // 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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;
         
         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, MessageBoxOptions::RightAlign );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }

private void validateUserEntry2()
{

    // 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 = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons,
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
            MessageBoxOptions.RightAlign);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}
Private Sub ValidateUserEntry2()


    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "No Server Name Specified"
        Dim Buttons As Integer = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays a MessageBox using the Question icon and specifying the No button as the default.

        Result = MessageBox.Show(Me, Message, Caption, MessageBoxButtons.YesNo, _
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)


        ' Gets the result of the MessageBox display.

        If Result = System.Windows.Forms.DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If

End Sub

注解

可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。 消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

消息框中最多可以有三个按钮。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

在指定对象前面显示一个消息框,并显示指定的文本、标题、按钮和图标。

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

示例

下面的代码示例演示如何显示 MessageBox 此重载 Show支持的选项。 验证字符串变量是否 ServerName为空后,该示例会显示一个 MessageBox,为用户提供取消操作的选项。 Show如果方法的返回值计算结果为Yes,则显示MessageBox该方法的窗体已关闭。

private:
   void validateUserEntry4()
   {
      // 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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;

         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons, MessageBoxIcon::Question );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }
private void validateUserEntry4()
{

    // 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 = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons,
            MessageBoxIcon.Question);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}
Private Sub ValidateUserEntry4()

    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "No Server Name Specified"
        Dim Buttons As Integer = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays a MessageBox using the Question icon and specifying the No button as the default.

        Result = MessageBox.Show(Me, Message, Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        ' Gets the result of the MessageBox display.

        If Result = System.Windows.Forms.DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If

End Sub

注解

可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。 消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

消息框中最多可以有三个按钮。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

显示一个消息框,其中包含指定的文本、标题、按钮、图标和默认按钮。

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

示例

下面的代码示例演示如何显示 MessageBox 此重载 Show支持的选项。 验证字符串变量是否 ServerName为空后,该示例会显示一个 MessageBox 带有问题框图标的选项,为用户提供取消操作的选项。 Show如果方法的返回值计算结果为Yes,则显示MessageBox该方法的窗体已关闭。

private:
   void validateUserEntry3()
   {
      // 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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;

         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1 );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }
private void validateUserEntry3()
{

    // 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 = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons,
        MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}
Private Sub ValidateUserEntry3()

    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "No Server Name Specified"
        Dim Buttons As Integer = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays a MessageBox using the Question icon and specifying the No button as the default.

        Result = MessageBox.Show(Me, Message, Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button1)

        ' Gets the result of the MessageBox display.

        If Result = System.Windows.Forms.DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If
End Sub

注解

消息框中最多可以有三个按钮。

另请参阅

适用于

Show(IWin32Window, String, String, MessageBoxButtons)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

在指定对象前面显示一个消息框,并带有指定的文本、标题和按钮。

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

示例

下面的代码示例演示如何显示 MessageBox 此重载 Show支持的选项。 验证字符串变量是否 ServerName为空后,该示例会显示一个 MessageBox,为用户提供取消操作的选项。 Show如果方法的返回值计算结果为Yes,则显示MessageBox该方法的窗体已关闭。

private:
   void validateUserEntry5()
   {
      // 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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;

         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }
private void validateUserEntry5()
{

    // 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 = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}
Private Sub ValidateUserEntry5()

    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "No Server Name Specified"
        Dim Buttons As Integer = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays a MessageBox using the Question icon and specifying the No button as the default.

        Result = MessageBox.Show(Me, Message, Caption, MessageBoxButtons.YesNo)

        ' Gets the result of the MessageBox display.

        If Result = System.Windows.Forms.DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If

End Sub

注解

可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。 消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

消息框中最多可以有三个按钮。

另请参阅

适用于

Show(String, String, MessageBoxButtons, MessageBoxIcon)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

返回

其中一个 DialogResult 值。

例外

buttons指定的参数不是 . 的MessageBoxButtons一个成员。

-或-

icon指定的参数不是 . 的MessageBoxIcon一个成员。

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

示例

下面的代码示例演示处理Show事件时的方法ComboBox.DropDown之一。 若要运行该示例,请将以下代码粘贴到窗体中,并从窗体的构造函数或InitializeComboBox方法调用Load该方法。

internal:
   // Declare ComboBox1
   System::Windows::Forms::ComboBox^ ComboBox1;

private:
   // Initialize ComboBox1.
   void InitializeComboBox()
   {
      this->ComboBox1 = gcnew ComboBox;
      this->ComboBox1->Location = System::Drawing::Point( 128, 48 );
      this->ComboBox1->Name = "ComboBox1";
      this->ComboBox1->Size = System::Drawing::Size( 100, 21 );
      this->ComboBox1->TabIndex = 0;
      this->ComboBox1->Text = "Typical";
      array<String^>^ installs = {"Typical","Compact","Custom"};
      ComboBox1->Items->AddRange( installs );
      this->Controls->Add( this->ComboBox1 );
      
      // Hook up the event handler.
      this->ComboBox1->DropDown += gcnew System::EventHandler(
         this, &Form1::ComboBox1_DropDown );
   }

   // Handles the ComboBox1 DropDown event. If the user expands the  
   // drop-down box, a message box will appear, recommending the
   // typical installation.
   void ComboBox1_DropDown( Object^ sender, System::EventArgs^ e )
   {
      MessageBox::Show( "Typical installation is strongly recommended.",
         "Install information", MessageBoxButtons::OK,
         MessageBoxIcon::Information );
   }

// Declare ComboBox1.
internal System.Windows.Forms.ComboBox ComboBox1;

// Initialize ComboBox1.
private void InitializeComboBox()
{
    this.ComboBox1 = new ComboBox();
    this.ComboBox1.Location = new System.Drawing.Point(128, 48);
    this.ComboBox1.Name = "ComboBox1";
    this.ComboBox1.Size = new System.Drawing.Size(100, 21);
    this.ComboBox1.TabIndex = 0;
    this.ComboBox1.Text	= "Typical";
    string[] installs = new string[]{"Typical", "Compact", "Custom"};
    ComboBox1.Items.AddRange(installs);
    this.Controls.Add(this.ComboBox1);
    
    // Hook up the event handler.
    this.ComboBox1.DropDown +=  
        new System.EventHandler(ComboBox1_DropDown);
}

// Handles the ComboBox1 DropDown event. If the user expands the  
// drop-down box, a message box will appear, recommending the
// typical installation.
private void ComboBox1_DropDown(object sender, System.EventArgs e)
{
    MessageBox.Show("Typical installation is strongly recommended.", 
    "Install information", MessageBoxButtons.OK, 
        MessageBoxIcon.Information);
}

' Declare ComboBox1.
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

' Initialize ComboBox1.
Private Sub InitializeComboBox()
    Me.ComboBox1 = New ComboBox
    Me.ComboBox1.Location = New System.Drawing.Point(128, 48)
    Me.ComboBox1.Name = "ComboBox1"
    Me.ComboBox1.Size = New System.Drawing.Size(100, 21)
    Me.ComboBox1.TabIndex = 0
    Me.ComboBox1.Text = "Typical"
    Dim installs() As String = New String() _
        {"Typical", "Compact", "Custom"}
    ComboBox1.Items.AddRange(installs)
    Me.Controls.Add(Me.ComboBox1)
End Sub

' Handles the ComboBox1 DropDown event. If the user expands the  
' drop-down box, a message box will appear, recommending the
' typical installation.
Private Sub ComboBox1_DropDown _ 
    (ByVal sender As Object, ByVal e As System.EventArgs) _ 
    Handles ComboBox1.DropDown
    MessageBox.Show("Typical installation is strongly recommended.", _
    "Install information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

注解

消息框中最多可以有三个按钮。

另请参阅

适用于

Show(IWin32Window, String, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

在指定对象前面显示一个消息框,并带有指定的文本和标题。

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption);
static member Show : System.Windows.Forms.IWin32Window * string * string -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

返回

其中一个 DialogResult 值。

注解

可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。 消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

默认情况下,消息框显示 “确定 ”按钮。

适用于

Show(String, String, MessageBoxButtons)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

返回

其中一个 DialogResult 值。

例外

buttons指定的参数不是 . 的MessageBoxButtons一个成员。

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

示例

下面的代码示例演示如何显示 MessageBox 此重载 Show支持的选项。 验证字符串变量是否 ServerName为空后,该示例会显示一个 MessageBox,为用户提供取消操作的选项。 Show如果方法的返回值计算结果为Yes,则显示MessageBox该方法的窗体已关闭。

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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;

         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }
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();
        }
    }
}
Private Sub ValidateUserEntry()
    ' Checks the value of the text.
    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.
        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "Error Detected in Input"
        Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays the MessageBox
        Result = MessageBox.Show(Message, Caption, Buttons)

        ' Gets the result of the MessageBox display.
        If Result = System.Windows.Forms.DialogResult.Yes Then
            ' Closes the parent form.
            Me.Close()
        End If
    End If
End Sub

注解

消息框中最多可以有三个按钮。

另请参阅

适用于

Show(IWin32Window, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

在指定对象前面显示一个消息框,并显示具有指定文本。

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text);
static member Show : System.Windows.Forms.IWin32Window * string -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

返回

其中一个 DialogResult 值。

注解

可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。 消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。

默认情况下,消息框显示 “确定 ”按钮。 消息框不包含标题。

适用于

Show(String, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption);
public static System.Windows.Forms.DialogResult Show(string text, string caption);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption);
static member Show : string * string -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

返回

其中一个 DialogResult 值。

注解

默认情况下,消息框显示 “确定 ”按钮。

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options);
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);
static member Show : string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions -> System.Windows.Forms.DialogResult
Public Shared Function Show (text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions) As DialogResult

参数

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示 MessageBox 此重载 Show支持的选项。 验证字符串变量是否 ServerName为空后,该示例会显示一个 MessageBox 带有问题框图标的选项,为用户提供取消操作的选项。 该示例使用 RightAlign 枚举的成员 MessageBoxOptions 将文本与对话框的右边缘对齐。 Show如果方法的返回值计算结果为Yes,则显示MessageBox该方法的窗体已关闭。

private:
   void validateUserEntry2()
   {
      // 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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;
         
         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, MessageBoxOptions::RightAlign );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }

private void validateUserEntry2()
{

    // 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 = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons,
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
            MessageBoxOptions.RightAlign);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}
Private Sub ValidateUserEntry2()


    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "No Server Name Specified"
        Dim Buttons As Integer = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays a MessageBox using the Question icon and specifying the No button as the default.

        Result = MessageBox.Show(Me, Message, Caption, MessageBoxButtons.YesNo, _
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)


        ' Gets the result of the MessageBox display.

        If Result = System.Windows.Forms.DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If

End Sub

注解

消息框中最多可以有三个按钮。

另请参阅

适用于

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

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

public:
 static System::Windows::Forms::DialogResult Show(System::Windows::Forms::IWin32Window ^ owner, System::String ^ text, System::String ^ caption, System::Windows::Forms::MessageBoxButtons buttons, System::Windows::Forms::MessageBoxIcon icon, System::Windows::Forms::MessageBoxDefaultButton defaultButton, System::Windows::Forms::MessageBoxOptions options, System::String ^ helpFilePath, System::Windows::Forms::HelpNavigator navigator, System::Object ^ param);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object param);
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object? param);
static member Show : System.Windows.Forms.IWin32Window * string * string * System.Windows.Forms.MessageBoxButtons * System.Windows.Forms.MessageBoxIcon * System.Windows.Forms.MessageBoxDefaultButton * System.Windows.Forms.MessageBoxOptions * string * System.Windows.Forms.HelpNavigator * obj -> System.Windows.Forms.DialogResult
Public Shared Function Show (owner As IWin32Window, text As String, caption As String, buttons As MessageBoxButtons, icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton, options As MessageBoxOptions, helpFilePath As String, navigator As HelpNavigator, param As Object) As DialogResult

参数

owner
IWin32Window

其实现 IWin32Window 将拥有模式对话框。

text
String

要显示在消息框中的文本。

caption
String

要显示在消息框标题栏中的文本。

buttons
MessageBoxButtons

指定要 MessageBoxButtons 在消息框中显示的按钮的值之一。

icon
MessageBoxIcon

指定要 MessageBoxIcon 在消息框中显示的图标的值之一。

defaultButton
MessageBoxDefaultButton

指定 MessageBoxDefaultButton 消息框的默认按钮的值之一。

options
MessageBoxOptions

指定 MessageBoxOptions 将用于消息框的显示和关联选项的值之一。 如果要使用默认值,可以传入 0。

helpFilePath
String

当用户单击“帮助”按钮时要显示的帮助文件的路径和名称。

navigator
HelpNavigator

其中一个 HelpNavigator 值。

param
Object

当用户单击“帮助”按钮时要显示的帮助主题的数字 ID。

返回

其中一个 DialogResult 值。

例外

buttons 不是 . 的成员 MessageBoxButtons

-或-

icon 不是 . 的成员 MessageBoxIcon

-或-

指定的 defaultButton 不是 . 的成员 MessageBoxDefaultButton

尝试在未在用户交互模式下运行的进程中显示 MessageBox 该进程。 此属性由 UserInteractive 该属性指定。

options DefaultDesktopOnly同时指定和 ServiceNotification.

-或-

buttons 指定了无效的组合 MessageBoxButtons

示例

下面的代码示例演示如何显示主窗口父级的消息框。 消息框显示“帮助”按钮。 当用户单击“帮助”按钮时, Mspaint.chm 将打开帮助文件,并显示由关键字标识的 ovals 帮助索引选项卡和主题。 该示例要求 Mspaint.chm 安装帮助文件。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file, 
// shows index with the "ovals" keyword selected, and displays the
// associated topic.
System::Windows::Forms::DialogResult r6 = MessageBox::Show( this, "Message with Help file and Help navigator with additional parameter.", "Help Caption", MessageBoxButtons::OK, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, (MessageBoxOptions)0, "mspaint.chm", HelpNavigator::KeywordIndex, "ovals" );
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file, 
// shows index with the "ovals" keyword selected, and displays the
// associated topic.
DialogResult r6 = MessageBox.Show (this, 
                                   "Message with Help file and Help navigator with additional parameter.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.KeywordIndex, "ovals");
' Display message box parented to the main form. 
' The Help button opens the Mspaint.chm Help file, 
' shows index with the "ovals" keyword selected, and displays the
' associated topic.
Dim r6 As DialogResult = MessageBox.Show(Me, _
                                   "Message with Help file and Help navigator with additional parameter.", _
                                   "Help Caption", MessageBoxButtons.OK, _
                                   MessageBoxIcon.Question, _
                                   MessageBoxDefaultButton.Button1, _
                                   0, "mspaint.chm", _
                                   HelpNavigator.KeywordIndex, "ovals")

注解

消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会发生任何输入(键盘或鼠标单击)。 程序必须隐藏或关闭模式窗体(通常是响应某些用户操作),然后才能输入到另一个窗体。 可以使用参数 owner 来指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。

当用户单击“帮助”按钮时,将打开参数中指定的 helpFilePath 帮助文件,并显示由该参数标识的 navigator 帮助内容。 拥有消息框(或活动窗体)的窗体也会接收该 HelpRequested 事件。

编译的帮助文件提供页面中的目录、索引、搜索和关键字链接。 可使用以下值:navigator、、TableOfContentsFindIndexTopic

可用于 param 进一步优化 Topic 命令。 如果参数中指定的navigator值为,则TableOfContents此值应为IndexFindnull此值。 如果参数 navigator 引用 Topic,此值应引用包含要显示的主题的数值的对象。

参数 helpFilePath 可以是 C:\path\sample.chm 或 /folder/file.htm。

另请参阅

适用于