VB.NET MsgBox and MessageBox.Show – Complete Guide (2026)
The VB.NET MsgBox function displays a message box and returns which button the user clicked. It is quick to use for alerts, confirmations, and yes/no prompts. This guide covers syntax, button combinations, icons, return values, the difference between MsgBox and MessageBox.Show, and when to use which.
Inhaltsverzeichnis
- 1 MsgBox vs. MessageBox.Show
- 2 MsgBox syntax
- 3 Simple message
- 4 Yes/No confirmation
- 5 Yes/No/Cancel
- 6 All button styles
- 7 All icon styles
- 8 Setting the default button
- 9 The same examples with MessageBox.Show
- 10 When to use which
- 11 Complete example: save-before-close pattern
- 12 Common mistakes
- 13 FAQ
- 14 Wrapping up
MsgBox vs. MessageBox.Show
VB.NET offers two ways to display a message box. Both open the same Windows dialog, but they differ in origin and API style:
' VB.NET-style (from Microsoft.VisualBasic namespace)
MsgBox("File saved.", MsgBoxStyle.Information, "Success")
' .NET Framework-style (from System.Windows.Forms)
MessageBox.Show("File saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
| Aspect | MsgBox | MessageBox.Show |
|---|---|---|
| Namespace | Microsoft.VisualBasic | System.Windows.Forms |
| Available in C# | No (VB.NET only) | Yes |
| Return type | MsgBoxResult | DialogResult |
| Button/icon enums | MsgBoxStyle (combinable) | Separate enums for buttons and icons |
| Parameter order | Message, Style, Title | Message, Title, Buttons, Icon |
MsgBox is a VB.NET convenience wrapper around the Win32 MessageBox API. MessageBox.Show is the .NET Framework class that works across all .NET languages. Both produce the exact same dialog window. If your project is purely VB.NET, either works fine. If you share code with C# developers or follow .NET conventions, MessageBox.Show is the better choice.
MsgBox syntax
Dim result As MsgBoxResult = MsgBox(
Prompt, ' The message text (required)
Buttons Or Icon, ' MsgBoxStyle flags (optional)
Title ' Window title (optional)
)
The first parameter is the message. The second combines button and icon styles with Or. The third sets the window title. Only the first parameter is required.
Simple message
The simplest MsgBox with just a message and the default OK button:
MsgBox("The file has been saved.")
With a title and information icon:
MsgBox("The file has been saved.", MsgBoxStyle.Information, "Success")
Yes/No confirmation
Ask the user a yes/no question and react to their choice:
Dim result = MsgBox(
"Do you want to delete this record?",
MsgBoxStyle.YesNo Or MsgBoxStyle.Question,
"Confirm deletion")
If result = MsgBoxResult.Yes Then
DeleteRecord()
End If
Combine button style and icon style with Or. The return value tells you which button was clicked.
Yes/No/Cancel
For unsaved changes, offer three options:
Dim result = MsgBox(
"You have unsaved changes. Save before closing?",
MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Exclamation,
"Unsaved changes")
Select Case result
Case MsgBoxResult.Yes
SaveDocument()
Me.Close()
Case MsgBoxResult.No
Me.Close()
Case MsgBoxResult.Cancel
' Do nothing, stay on the form
End Select
| MsgBoxStyle | Buttons shown | Possible results |
|---|---|---|
OKOnly (default) | OK | Ok |
OKCancel | OK, Cancel | Ok, Cancel |
YesNo | Yes, No | Yes, No |
YesNoCancel | Yes, No, Cancel | Yes, No, Cancel |
RetryCancel | Retry, Cancel | Retry, Cancel |
AbortRetryIgnore | Abort, Retry, Ignore | Abort, Retry, Ignore |
All icon styles
| MsgBoxStyle | Icon | Use case |
|---|---|---|
Information | Blue info circle | Status messages, confirmations |
Question | Blue question mark | Yes/No prompts |
Exclamation | Yellow warning triangle | Warnings, unsaved changes |
Critical | Red error circle | Errors, critical failures |
By default, the first button is focused. You can change this to prevent accidental clicks on a destructive action:
' Focus "No" by default so the user doesn't accidentally delete
Dim result = MsgBox(
"Delete all records permanently?",
MsgBoxStyle.YesNo Or MsgBoxStyle.Critical Or MsgBoxStyle.DefaultButton2,
"Warning")
If result = MsgBoxResult.Yes Then
DeleteAllRecords()
End If
DefaultButton2 focuses the second button (No). Use DefaultButton3 for the third button in three-button dialogs.
The same examples with MessageBox.Show
Here are the same patterns using the .NET MessageBox.Show equivalent, so you can compare:
' Simple message
MessageBox.Show("The file has been saved.", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Yes/No confirmation
Dim result As DialogResult = MessageBox.Show(
"Do you want to delete this record?", "Confirm deletion",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
DeleteRecord()
End If
' Yes/No/Cancel with default button
Dim result2 As DialogResult = MessageBox.Show(
"Save before closing?", "Unsaved changes",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2)
Select Case result2
Case DialogResult.Yes
SaveDocument()
Me.Close()
Case DialogResult.No
Me.Close()
Case DialogResult.Cancel
' Stay on the form
End Select
The functionality is identical. MessageBox.Show uses separate enums (MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) instead of combining flags with Or. The return type is DialogResult instead of MsgBoxResult.
When to use which
Use MsgBox when:
- Your project is VB.NET only and you prefer shorter syntax
- You are prototyping quickly and want minimal code
Use MessageBox.Show when:
- Your codebase is shared with C# developers
- You want to follow .NET conventions
- You need the owner window parameter (to keep the dialog on top of a specific form)
- You want your code to be portable to .NET 5+ / .NET 8+ projects
Complete example: save-before-close pattern
A realistic example that asks to save before closing a form. This pattern works in any text editor, data entry form, or settings window:
Private _hasChanges As Boolean = False
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
_hasChanges = True
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Not _hasChanges Then Return
Dim result = MsgBox(
"You have unsaved changes. Save now?",
MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Exclamation Or MsgBoxStyle.DefaultButton1,
"Unsaved changes")
Select Case result
Case MsgBoxResult.Yes
SaveToFile()
Case MsgBoxResult.Cancel
e.Cancel = True ' Prevent closing
End Select
End Sub
Private Sub SaveToFile()
File.WriteAllText("data.txt", TextBox1.Text, Encoding.UTF8)
_hasChanges = False
End Sub
Setting e.Cancel = True in the FormClosing event prevents the form from closing. The user stays on the form and can continue editing. For more on writing files, see the VB.NET Write Text File guide.
Common mistakes
Ignoring the return value
' BAD - asks Yes/No but ignores the answer
MsgBox("Delete?", MsgBoxStyle.YesNo)
DeleteRecord()
' GOOD - check which button was clicked
If MsgBox("Delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
DeleteRecord()
End If
Confusing MsgBox with VBScript MsgBox
The VB.NET MsgBox function lives in Microsoft.VisualBasic.Interaction and returns MsgBoxResult. The VBScript MsgBox returns integer values (1 for OK, 6 for Yes, etc.). They look similar but live in completely different environments. In VB.NET, always compare against the MsgBoxResult enum, never against magic numbers.
Using MsgBox in a background thread
Both MsgBox and MessageBox.Show must be called from the UI thread. If you call them from a background thread or Task, the dialog may appear behind other windows or cause cross-thread exceptions. Use Me.Invoke() to marshal the call to the UI thread if needed.
FAQ
MsgBox is a VB.NET convenience function from the Microsoft.VisualBasic namespace. MessageBox.Show is the .NET Framework class from System.Windows.Forms. Both produce the same dialog. MsgBox uses MsgBoxResult, while MessageBox.Show uses DialogResult.
Use MsgBox("Your question", MsgBoxStyle.YesNo) and compare the result to MsgBoxResult.Yes or MsgBoxResult.No. Add Or MsgBoxStyle.Question for a question mark icon.
Add MsgBoxStyle.DefaultButton2 or DefaultButton3 to the style parameter. This moves the initial focus to the second or third button, preventing accidental clicks on destructive actions.
No. VB.NET MsgBox returns a typed MsgBoxResult enum. VBScript MsgBox returns integer constants (1 for OK, 6 for Yes, etc.). They look similar but run in completely different environments.
Not directly. Both MsgBox and MessageBox.Show should be called from the UI thread. From a background thread, use Me.Invoke() to marshal the call to the UI thread first.
Wrapping up
The MsgBox function gives you quick message dialogs with one line of code. Combine button styles and icon styles with Or, always check the return value for Yes/No prompts, and use DefaultButton2 before destructive actions. If you prefer .NET-standard code, use MessageBox.Show with separate enums for the same result. For handling button events dynamically, see the VB.NET AddHandler guide. For writing files after a save prompt, check the VB.NET Write Text File guide.