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.

Need .NET help?

Building a desktop application?

I've been developing professionally in VB.NET and C# for over 17 years. From message boxes to complete WinForms applications, I can help.

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)
AspectMsgBoxMessageBox.Show
NamespaceMicrosoft.VisualBasicSystem.Windows.Forms
Available in C#No (VB.NET only)Yes
Return typeMsgBoxResultDialogResult
Button/icon enumsMsgBoxStyle (combinable)Separate enums for buttons and icons
Parameter orderMessage, Style, TitleMessage, 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

All button styles

MsgBoxStyleButtons shownPossible results
OKOnly (default)OKOk
OKCancelOK, CancelOk, Cancel
YesNoYes, NoYes, No
YesNoCancelYes, No, CancelYes, No, Cancel
RetryCancelRetry, CancelRetry, Cancel
AbortRetryIgnoreAbort, Retry, IgnoreAbort, Retry, Ignore

All icon styles

MsgBoxStyleIconUse case
InformationBlue info circleStatus messages, confirmations
QuestionBlue question markYes/No prompts
ExclamationYellow warning triangleWarnings, unsaved changes
CriticalRed error circleErrors, critical failures
Planning a project?

Need a polished WinForms application?

From message dialogs to complete data-driven desktop apps: I design software that lasts. Let's talk about your project.

Setting the default button

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.

Need expert support?

Looking for an experienced .NET developer?

I'll take on your project, from user dialogs to finished desktop applications. Just drop me a message.

FAQ

What is the difference between MsgBox and MessageBox.Show?

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.

How do I show a Yes/No message box in VB.NET?

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.

How do I change the default button in MsgBox?

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.

Is VB.NET MsgBox the same as VBScript MsgBox?

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.

Can I use MsgBox in a background thread?

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.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Robert Skibbe
Datenschutz-Übersicht

Diese Website verwendet Cookies, damit wir dir die bestmögliche Benutzererfahrung bieten können. Cookie-Informationen werden in deinem Browser gespeichert und führen Funktionen aus, wie das Wiedererkennen von dir, wenn du auf unsere Website zurückkehrst, und hilft unserem Team zu verstehen, welche Abschnitte der Website für dich am interessantesten und nützlichsten sind.