Visual Basic MsgBox

The Visual Basic MsgBox is a simple and commonly used function for displaying messages to the user. The message window is a dialog box and can have specific buttons for user interaction. When the user click on a button, an integer value is returned by the function indicating which button was pressed.

MsgBox Syntax

'VB 6.0
MsgBox (prompt, [buttons], [title], [help file], [context])

MsgBox (prompt, [button As VbMsgBoxStyle = VbOkOnly], [title], [help file], [context])

MsgBox Arguments

This argument is a string message that you want to display in the message dialog box. Except for this argument, all others are optional.

This argument defines the buttons to be displayed; if no value is passed then the OK button is displayed by default. Other buttons like Yes, No, Cancel, Abort, Retry and Ignore can be displayed by passing the corresponding value to this argument.

The Visual Basic Msgbox function can also display an icon beside the message. You can display icons such as Information, Exclamation, Question and Critical by passing the corresponding icon value along with the button argument.

This argument holds the string to be displayed in the title bar of the dialog box. If you do not specify a title, the application name is placed in the title bar.

Help File
This argument is a string that identifies the help file to be used to provide context-sensitive help for the dialog box.

This argument is the help context number assigned to the appropriate help topic.

VB 6.0 Button Argument and Return Values

vbOKOnly 0
vbOKCancel 1
vbAbortRetryIgnore 2
vbYesNoCancel 3
vbYesNo  4
vbRetryCancel 5
vbCritical 16
vbQuestion 32
vbExclamation 48
vbInformation 64
Return Values
vbOK 1
vbCancel 2
vbAbort 3
vbRetry 4
vbIgnore 5
vbYes 6
vbNo 7

VB 6.0 Example

Dim iReturnValue As Integer
iReturnValue = MsgBox("Do you want to continue?", vbYesNo + vbQuestion)
If iReturnValue = vbYes Then
    'Continue next step
End If

VB.NET Button Argument and Return Values

In Visual Basic .NET, the MsgBoxStyle class is used to set the Button argument. For the results, the MsgBoxResults class is used.

MsgBoxStyle.OKOnly 0
MsgBoxStyle.OKCancel 1
MsgBoxStyle.AbortRetryIgnore 2
MsgBoxStyle.YesNoCancel  3
MsgBoxStyle.YesNo 4
MsgBoxStyle.RetryCancel 5
MsgBoxStyle.Critical 16
MsgBoxStyle.Question 32
MsgBoxStyle.Exclamation 48
MsgBoxStyle.Information 64
Return Values
MsgBoxResult.OK 1
MsgBoxResult.Cancel 2
MsgBoxResult.Abort 3
MsgBoxResult.Retry 4
MsgBoxResult.Ignore 5
MsgBoxResult.Yes 6
MsgBoxResult.No 7

Visual Basic .NET Example

Dim iReturnValue As Integer
iReturnValue = MsgBox("Do you want to continue?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
If iReturnValue = MsgBoxResult.Yes Then
   'Continue next step
End If
The Visual Basic MsgBox function provides a simple way to interact with the users and get inputs (via button selection). Additionally, displaying an icon along side the message creates an intuitive user experience.

Back to Visual Basic Help