Displays a message in a dialog box, waits for the user to click a button, and then returns an integer indicating which button the user clicked.
Assembly: Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, Resources = HostProtectionResource.UI)] public static MsgBoxResult MsgBox( Object Prompt, MsgBoxStyle Buttons, Object Title )
- Type: System.Object
Required. String expression displayed as the message in the dialog box. The maximum length of Prompt is approximately 1024 characters, depending on the width of the characters used. If Prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)), a line feed character (Chr(10)), or a carriage return/linefeed character combination (Chr(13) & Chr(10)) between each line.
- Type: Microsoft.VisualBasic.MsgBoxStyle
Optional. Numeric expression 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 modality of the message box. If you omit Buttons, the default value is zero.
- Type: System.Object
Optional. String expression displayed in the title bar of the dialog box. If you omit Title, the application name is placed in the title bar.
Return ValueType: Microsoft.VisualBasic.MsgBoxResult
If you omit any positional arguments, you must retain the corresponding comma delimiter.
If the dialog box displays a Cancel button, pressing the ESC key has the same effect as clicking Cancel
If the dialog box contains a Help button, context-sensitive Help is provided for the dialog box. However, no value is returned until one of the other buttons is chosen. In Windows Form applications, choosing the Help button executes the HelpRequested event for the form.
The MsgBoxStyle enumeration values are listed in the following table.
Displays OK button only.
Displays OK and Cancel buttons.
Displays Abort, Retry, and Ignore buttons.
Displays Yes, No, and Cancel buttons.
Displays Yes and No buttons.
Displays Retry and Cancel buttons.
Displays Critical Message icon.
Displays Warning Query icon.
Displays Warning Message icon.
Displays Information Message icon.
First button is default.
Second button is default.
Third button is default.
Application is modal. The user must respond to the message box before continuing work in the current application.
System is modal. All applications are suspended until the user responds to the message box.
Specifies the message box window as the foreground window.
Text is right-aligned.
Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.
The first group of values (0–5) describes the number and type of buttons displayed in the dialog box. The second group (16, 32, 48, 64) describes the icon style. The third group (0, 256, 512) determines which button is the default. The fourth group (0, 4096) determines the modality of the message box, and the fifth group specifies whether or not the message box window is the foreground window, along with the alignment and direction of the text. When adding numbers to create a final value for the Buttons argument, use only one number from each group.
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Resources. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
This example uses the MsgBox function to display a critical-error message in a dialog box with Yes and No buttons. The No button is specified as the default response. This is done by combining the MsgBox constant values into one numeric expression. In this case, adding 4 (the Yes/No button combination) and 16 (the Critical Message window) and 256 (the second button as default button) gives a total of 276. The value returned by the MsgBox function depends on the button chosen by the user: Yes returns a value of 6; No returns a value of 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
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.