VBScript MsgBoxFunction


VBScript 参考手册Complete VBScript Reference

The MsgBox function displays a message box, waits for the user to click a button, and then returns a value indicating which button was clicked.

The MsgBox function returns the following values:

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

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

Tip:See the InputBox function.

Syntax

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

Parameters Description
prompt Required. A string expression displayed as a message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used. If prompt contains multiple lines, you can separate the lines using carriage return characters (Chr(13)), linefeed characters (Chr(10)), or a combination of carriage return and linefeed characters (Chr(13) & Chr(10)).
buttons Optional. A numeric value that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the style of the message box. The default value is 0.
  • 0 = vbOKOnly - Display OK button only
  • 1 = vbOKCancel - Display OK and Cancel buttons
  • 2 = vbAbortRetryIgnore - Display Abort, Retry, and Ignore buttons
  • 3 = vbYesNoCancel - Display Yes, No, and Cancel buttons
  • 4 = vbYesNo - Display Yes and No buttons
  • 5 = vbRetryCancel - Display Retry and Cancel buttons
  • 16 = vbCritical - Display Critical Message icon
  • 32 = vbQuestion - Display Warning Query icon
  • 48 = vbExclamation - Display Warning Message icon
  • 64 = vbInformation - Display Information Message icon
  • 0 = vbDefaultButton1 - First button is default
  • 256 = vbDefaultButton2 - Second button is default
  • 512 = vbDefaultButton3 - Third button is default
  • 768 = vbDefaultButton4 - Fourth button is default
  • 0 = vbApplicationModal - Application modal (the user must respond to the message box before continuing to work in the current application)
  • 4096 = vbSystemModal - System modal (all applications are suspended until the user responds to the message box)

We can separate the buttons into four groups: the first group of values (0-5) is used to describe the type and number of buttons displayed in the dialog box; the second group (16,32,48,64) is used to describe the icon style; the third group (0,256,512,768) is used to determine the default button; and the fourth group (0,4096) determines the style of the message box. When adding these numbers together to generate the buttons parameter value, you can only use one number from each group.

title Optional. The title of the message box. The default is the application name.
helpfile Optional. A string expression that identifies the help file used to provide context-sensitive help for the dialog box. Must be used together with the context parameter.
context Optional. A numeric expression that identifies the context number assigned to a help topic by the help file author. Must be used together with the helpfile parameter.

Examples

Example 1

<script type="text/vbscript">

MsgBox("Hello world")

</script>

Try it Yourself »

Example 2

Message box with line breaks:

<script type="text/vbscript">

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

</script>

Try it Yourself »

Example 3

Different buttonsets and different icons. Return the value of the clicked button:

<script type="text/vbscript">

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

</script>

Try it Yourself »

Example 4

Message box with a title:

<script type="text/vbscript">

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

</script>

Try it Yourself »


VBScript 参考手册Complete VBScript Reference Other extensions