在对话框中显示消息,等待用户单击按钮,然后返回一个整数,该整数指示用户单击的按钮。
命名空间:
Microsoft.VisualBasic
程序集:Microsoft.VisualBasic(在 Microsoft.VisualBasic.dll 中)
<HostProtectionAttribute(SecurityAction.LinkDemand,Resources := HostProtectionResource.UI)> _
Public Shared Function MsgBox ( _
Prompt As Object,_
Buttons As MsgBoxStyle,_
Title As Object _
) As MsgBoxResult
参数
-
Prompt
-
类型:
System.Object
必需。作为消息显示在对话框中的
String表达式。
Prompt的最大长度大约为 1024 个字符,具体取决于所用字符的宽度。如果
Prompt包含多行,您可以在各行之间使用回车符(
Chr(13
))、换行符(
Chr(10
))或回车/换行符的组合(
Chr(13
)&
Chr(10
))分隔各行。
-
Buttons
-
类型:
Microsoft.VisualBasic.MsgBoxStyle
可选。数值表达式,它是值的总和,指定显示的按钮数目及按钮类型,使用的图标样式,默认按钮的标识以及消息框的样式等。如果省略
Buttons,则默认值为 0。
-
Title
-
类型:
System.Object
可选。显示在对话框标题栏中的
String表达式。如果省略
Title,则标题栏中显示应用程序名称。
返回值
类型:
Microsoft.VisualBasic
.MsgBoxResult
常量 |
值 |
OK |
1 |
Cancel |
2 |
Abort |
3 |
Retry |
4 |
Ignore |
5 |
Yes |
6 |
No |
7 |
异常 |
条件 |
ArgumentException |
Prompt不是一个String表达式,或者Title无效。 |
InvalidOperationException |
进程不是以 User Interactive 模式运行。 |
InvalidEnumArgumentException |
一个或多个参数,而不是MsgBoxResult或MsgBoxStyle枚举的成员。 |
如果对话框显示“取消”按钮,则按 Esc 键与单击“取消”效果相同。如果对话框包含“帮助”按钮,则为对话框提供区分上下文的帮助。但是,其他按钮中有一个被单击之前,都不会返回任何值。
说明 |
若要除第一个参数外还指定其他参数,必须在表达式中使用MsgBox函数。如果省略任何位置参数,则必须保留相应的逗号分隔符。 |
MsgBox函数需要SafeTopLevelWindows级别的UIPermission,该权限在部分受信任的情况下会对执行有影响。有关更多信息,请参见请求权限和UIPermission。
下表中列出MsgBoxStyle枚举值。
成员
Value |
说明 |
OKOnly |
0 |
仅显示“确定”按钮。 |
OKCancel |
1 |
显示“确定”和“取消”按钮。 |
AbortRetryIgnore |
2 |
显示“中止”、“重试”和“忽略”按钮。 |
YesNoCancel |
3 |
显示“是”、“否”和“取消”按钮。 |
YesNo |
4 |
显示“是”和“否”按钮。 |
RetryCancel |
5 |
显示“重试”和“取消”按钮。 |
Critical |
16 |
显示“重要消息”图标。 |
Question |
32 |
显示“警告疑问”图标。 |
Exclamation |
48 |
显示“警告消息”图标。 |
Information |
64 |
显示“信息消息”图标。 |
DefaultButton1 |
第一个按钮为默认按钮。 |
DefaultButton2 |
256 |
第二个按钮为默认按钮。 |
DefaultButton3 |
512 |
第三个按钮为默认按钮。 |
ApplicationModal |
应用程序为模式。在当前应用程序中继续工作之前用户必须对消息框进行响应。 |
SystemModal |
4096 |
系统是有模式的。所有应用程序都被挂起,直到用户响应消息框。 |
MsgBoxSetForeground |
65536 |
将消息框窗口指定为前台窗口。 |
MsgBoxRight |
524288 |
文本为右对齐。 |
MsgBoxRtlReading |
1048576 |
指定文本在希伯来语和阿拉伯语系统上应显示为从右向左的阅读方式。 |
第一组值 (0–5) 描述对话框中显示的按钮数量和类型。第二组值 (16,32,48,64) 描述图标样式。第三组值 (0,256,512) 确定默认使用哪个按钮。第四组值 (0,4096) 确定消息框的模式性,第五组值指定消息框窗口是否为前台窗口,以及文本对齐和方向。当添加数字以创建Buttons参数的最终值时,在每组数字中只能使用一个数字。
应用到此类型或成员的HostProtectionAttribute特性具有以下Resources属性值:Resources。TheHostProtectionAttributedoes not affect desktop applications (which are typically started by double-clicking an icon,typing a command,or entering a URL in a browser).有关更多信息,请参见HostProtectionAttribute类或SQL Server 编程和宿主保护特性。
此示例使用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