Interaction.MsgBox(Object, MsgBoxStyle, Object) 方法

定义

在对话框中显示消息,等待用户单击按钮,然后返回一个整数,指示用户单击的按钮。

public static Microsoft.VisualBasic.MsgBoxResult MsgBox(object Prompt, Microsoft.VisualBasic.MsgBoxStyle Buttons = Microsoft.VisualBasic.MsgBoxStyle.OkOnly, object? Title = default);
public static Microsoft.VisualBasic.MsgBoxResult MsgBox(object Prompt, Microsoft.VisualBasic.MsgBoxStyle Buttons = Microsoft.VisualBasic.MsgBoxStyle.ApplicationModal, object Title = default);
static member MsgBox : obj * Microsoft.VisualBasic.MsgBoxStyle * obj -> Microsoft.VisualBasic.MsgBoxResult
Public Function MsgBox (Prompt As Object, Optional Buttons As MsgBoxStyle = Microsoft.VisualBasic.MsgBoxStyle.OkOnly, Optional Title As Object = Nothing) As MsgBoxResult
Public Function MsgBox (Prompt As Object, Optional Buttons As MsgBoxStyle = Microsoft.VisualBasic.MsgBoxStyle.ApplicationModal, Optional Title As Object = Nothing) As MsgBoxResult

参数

Prompt
Object

必填。 String 表达式显示为对话框中的消息。 最大长度 Prompt 约为 1024 个字符,具体取决于所使用的字符的宽度。 如果Prompt包含多个行,则可以使用回车符(13Chr()、换行符()Chr(10))或每行之间的回车符/换行符组合(Chr(13)Chr(10))分隔行。

Buttons
MsgBoxStyle

Optional. 数值表达式,该值是指定要显示的按钮的数量和类型、要使用的图标样式、默认按钮的标识以及消息框的形式的值之和。 如果省略 Buttons,则默认值为零。

Title
Object

Optional. String 表达式显示在对话框的标题栏中。 如果省略 Title,应用程序名称将放在标题栏中。

返回

以下值之一:

恒定价值
OK1
Cancel2
Abort3
Retry4
Ignore5
Yes6
No7

例外

Prompt 不是 String 表达式,或者 Title 无效。

进程未在用户交互模式下运行。

一个或多个参数不是或MsgBoxResult枚举的成员MsgBoxStyle

示例

此示例使用 MsgBox 函数在对话框中显示带有“是”和“否”按钮的严重错误消息。 “否”按钮指定为默认响应。 这是通过将常量值合并 MsgBox 为一个数值表达式来完成的。 在这种情况下,添加 4 (是/否按钮组合) 和 16 ( 关键消息 窗口) 和 256 (第二个按钮作为默认按钮) 总共提供 276。 函数返回 MsgBox 的值取决于用户选择的按钮:是返回值 6;不返回值 7。

' The following example requires that Option Infer be set to On.

' Define the message you want to see inside the message box.
Dim msg = "Do you want to continue?"

' Display a simple message box.
MsgBox(msg)

' Define a title for the message box.
Dim title = "MsgBox Demonstration"

' Add the title to the display.
MsgBox(msg, , title)

' Now define a style for the message box. In this example, the
' message box will have Yes and No buttons, the default will be
' the No button, and a Critical Message icon will be present.
Dim style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or
            MsgBoxStyle.Critical

' Display the message box and save the response, Yes or No.
Dim response = MsgBox(msg, style, title)

' Take some action based on the response.
If response = MsgBoxResult.Yes Then
    MsgBox("YES, continue!!", , title)
Else
    MsgBox("NO, stop!!", , title)
End If

注解

如果省略任何位置参数,则必须保留相应的逗号分隔符。

如果对话框显示“取消”按钮,按 ESC 键的效果与单击“取消”的效果相同

如果对话框包含 “帮助 ”按钮,则会为该对话框提供上下文相关帮助。 但是,在选择其他按钮之一之前,不会返回任何值。 在 Windows 窗体应用程序中,选择 Help 按钮将执行窗体的 HelpRequested 事件。

注释

MsgBox 函数需要在 UIPermission 级别上 SafeTopLevelWindows ,这可能会影响其在部分信任情况下的执行。 有关详细信息,请参阅 UIPermission

MsgBoxStyle下表中列出了枚举值。

成员 价值 Description
成员 价值 Description
OKOnly 0 仅显示“确定”按钮。
OKCancel 1 显示“确定”和“取消”按钮。
AbortRetryIgnore 2 显示中止、重试和忽略按钮。
YesNoCancel 3 显示“是”、“否”和“取消”按钮。
YesNo 4 显示“是”和“否”按钮。
RetryCancel 5 显示“重试”和“取消”按钮。
Critical 16 显示“关键消息”图标。
Question 32 显示警告查询图标。
Exclamation 48 显示警告消息图标。
Information 64 显示信息消息图标。
DefaultButton1 0 默认为“第一个”按钮。
DefaultButton2 256 默认为“第二个”按钮。
DefaultButton3 512 第三个按钮为默认按钮。
ApplicationModal 0 应用程序是模式的。 在当前应用程序中继续工作之前,用户必须响应消息框。
SystemModal 4096 系统为模式。 在用户响应消息框之前,所有应用程序都将暂停。
MsgBoxSetForeground 65536 将消息框窗口指定为前景窗口。
MsgBoxRight 524288 文本右对齐。
MsgBoxRtlReading 1048576 指定文本在希伯来语和阿拉伯语系统中应从右到左显示。

第一组值(0-5)描述了对话框中显示的按钮的数量和类型。 第二组(16、32、48、64)描述了图标样式。 第三组 (0, 256, 512) 确定哪个按钮是默认值。 第四组(0,4096)确定消息框的模式,第五个组指定消息框窗口是否为前景窗口,以及文本的对齐和方向。 为参数添加数字以创建最终值 Buttons 时,请仅使用每个组中的一个数字。

适用于

另请参阅