Latest web development tutorials

VBScript MsgBox Function

VBScript Reference Complete VBScript Reference

MsgBox function displays a message box and wait for the user clicks a button, and returns a value indicating the button is clicked.

MsgBox function returns the following values:

  • 1 = vbOK - OK button is clicked
  • 2 = vbCancel - Cancel button is clicked
  • 3 = vbAbort - Abort button is clicked
  • 4 = vbRetry - Retry button is clicked
  • 5 = vbIgnore - Ignore button is clicked
  • 6 = vbYes - Yes button is clicked
  • 7 = vbNo - No button is clicked

Note: When the helpfile and context parameters are specified, users press the F1 key to view the help.

Tip: See the InputBox function.

grammar

MsgBox(prompt[,buttons][,title][,helpfile,context])

参数 描述
prompt 必需。作为消息显示在对话框中的字符串表达式。prompt 的最大长度大约是 1024 个字符,这取决于所使用的字符的宽度。如果 prompt 中包含多个行,则可在各行之间用回车符(Chr(13))、换行符(Chr(10))或回车换行符的组合(Chr(13) & Chr(10))分隔各行。
buttons 可选,是表示指定显示按钮的数目和类型、使用的图标样式,默认按钮的标识以及消息框样式的数值的总和。默认值为 0。
  • 0 = vbOKOnly - 只显示 OK 按钮
  • 1 = vbOKCancel - 显示 OK 和 Cancel 按钮
  • 2 = vbAbortRetryIgnore - 显示 Abort、Retry 和 Ignore 按钮
  • 3 = vbYesNoCancel - 显示 Yes、No 和 Cancel 按钮
  • 4 = vbYesNo - 显示 Yes 和 No 按钮
  • 5 = vbRetryCancel - 显示 Retry 和 Cancel 按钮
  • 16 = vbCritical - 显示临界信息图标
  • 32 = vbQuestion - 显示警告查询图标
  • 48 = vbExclamation - 显示警告消息图标
  • 64 = vbInformation - 显示信息消息图标
  • 0 = vbDefaultButton1 - 第一个按钮为默认按钮
  • 256 = vbDefaultButton2 - 第二个按钮为默认按钮
  • 512 = vbDefaultButton3 - 第三个按钮为默认按钮
  • 768 = vbDefaultButton4 - 第四个按钮为默认按钮
  • 0 = vbApplicationModal - 应用程序模式(用户必须响应消息框才能继续在当前应用程序中工作)
  • 4096 = vbSystemModal - 系统模式(在用户响应消息框前,所有应用程序都被挂起)

我们可以把按钮分成四组:第一组值(0-5)用于描述对话框中显示的按钮类型与数目;第二组值(16,32,48,64)用于描述图标的样式;第三组值(0,256,512,768)用于确定默认按钮;而第四组值(0,4096)则决定消息框的样式。在将这些数字相加以生成 buttons 参数值时,只能从每组值中取用一个数字。

title 可选。消息框的标题。默认是应用程序的名称。
helpfile 可选。字符串表达式,用于标识为对话框提供上下文相关帮助的帮助文件。必须与 context 参数一起使用。
context 可选。数值表达式,用于标识由帮助文件的作者指定给某个帮助主题的上下文编号。必须与 helpfile 参数一起使用。

Examples

Example 1

<script type="text/vbscript">

MsgBox("Hello world")

</script>

try it"

Example 2

Message box with line breaks:

<script type="text/vbscript">

MsgBox("Hello" & chr(13) & "world")

</script>

try it"

Example 3

Different buttonsets and different icons. Click on the return value of the button:

<script type="text/vbscript">

x=MsgBox("Hello world",n)
document.getElementById("myDiv").innerHTML="You clicked: " & x

</script>

try it"

Example 4

Message box with the title:

<script type="text/vbscript">

x=MsgBox("Are you a programmer",4,"Please answer")

</script>

try it"


VBScript Reference Complete VBScript Reference