14

Is there a difference between the following two?

 msgbox()
 messagebox.show()

Some tutorials use msgbox(), and some use the other, messagebox.show()---I see that both can have an editable style, but I was wondering: Why are there two?

Is it to accommodate older programmers (who have learnt on an older version of Visual Basic)?

So in that case, which one should I use in Visual Basic 2010 (Visual Studio 2010)?

6 Answers 6

14

MsgBox() is the same as Messagebox.Show().

It exists for VB6 programmers who are used to it.

There are no rules on which one to use, but since MsgBox simply ends up delegating to MessageBox, I personally would go directly with MessageBox.

2
  • 2
    +1. Although it mainly exists for backwards compatibility with existing working VB6 code. Also - sheesh! C# programmers are verbose :)
    – MarkJ
    Nov 1, 2012 at 12:15
  • 1
    I'm finding that I can't call MessageBox from a non-GUI library. It would seem that I would need to reference/import System.Windows.Forms to use it in the library, but that would spoil the reason I'm using a library. MsgBox works fine (to relay information to the parent GUI application), so there's at least one difference. Aug 14, 2015 at 17:30
9

Here is the source code for MsgBox(). As you can see it doesn't do anything particularly interesting before calling MessageBox.Show().

<MethodImpl(MethodImplOptions.NoInlining), HostProtection(SecurityAction.LinkDemand, Resources:=HostProtectionResource.UI)> _
Public Shared Function MsgBox(ByVal Prompt As Object, ByVal Optional Buttons As MsgBoxStyle = 0, ByVal Optional Title As Object = new Object()) As MsgBoxResult
    Dim owner As IWin32Window = Nothing
    Dim text As String = Nothing
    Dim titleFromAssembly As String
    Dim vBHost As IVbHost = HostServices.VBHost
    If (Not vBHost Is Nothing) Then
        owner = vBHost.GetParentWindow
    End If
    If ((((Buttons And 15) > MsgBoxStyle.RetryCancel) OrElse ((Buttons And 240) > MsgBoxStyle.Information)) OrElse ((Buttons And &HF00) > MsgBoxStyle.DefaultButton3)) Then
        Buttons = MsgBoxStyle.OkOnly
    End If
    Try 
        If (Not Prompt Is Nothing) Then
            [text] = CStr(Conversions.ChangeType(Prompt, GetType(String)))
        End If
    Catch exception As StackOverflowException
        Throw exception
    Catch exception2 As OutOfMemoryException
        Throw exception2
    Catch exception3 As ThreadAbortException
        Throw exception3
    Catch exception9 As Exception
        Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", New String() { "Prompt", "String" }))
    End Try
    Try 
        If (Title Is Nothing) Then
            If (vBHost Is Nothing) Then
                titleFromAssembly = Interaction.GetTitleFromAssembly(Assembly.GetCallingAssembly)
            Else
                titleFromAssembly = vBHost.GetWindowTitle
            End If
        Else
            titleFromAssembly = Conversions.ToString(Title)
        End If
    Catch exception4 As StackOverflowException
        Throw exception4
    Catch exception5 As OutOfMemoryException
        Throw exception5
    Catch exception6 As ThreadAbortException
        Throw exception6
    Catch exception13 As Exception
        Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", New String() { "Title", "String" }))
    End Try
    Return DirectCast(MessageBox.Show(owner, [text], titleFromAssembly, (DirectCast(Buttons, MessageBoxButtons) And DirectCast(15, MessageBoxButtons)), (DirectCast(Buttons, MessageBoxIcon) And DirectCast(240, MessageBoxIcon)), (DirectCast(Buttons, MessageBoxDefaultButton) And DirectCast(&HF00, MessageBoxDefaultButton)), (DirectCast(Buttons, MessageBoxOptions) And DirectCast(-4096, MessageBoxOptions))), MsgBoxResult)
End Function

 

 
3
  • So you are trying to say that MsgBox is the same as messagebox and can be used either? Jan 10, 2012 at 8:10
  • 1
    Of course either can be used. But for professional development it is considered poor form to use MsgBox. Jan 10, 2012 at 8:11
  • 3
    Also, MsgBox will attempt cast everything you throw at it (Since the value taken is Object, and it'll throw an exception at runtime if any error happens when casting, although msdn states that you must provide a string to it..) while Messagebox.Show is more "strict", accepting only String value. Since MsgBox will call Messagebox.Show anyways, why take the "slow-route?" Apr 29, 2014 at 23:27
6

There is a difference when you are attempting to mix icons with different buttons. MsgBox has predefined styles (there may be a way to create new styles).

For example:

MsgBox("Do you wish to save changes?", MsgBoxStyle.YesNoCancel, "Save Changes")

enter image description here

^ This will display a box with Yes, No and Cancel buttons without an icon.



MsgBox("Do you wish to save changes?", MsgBoxStyle.Question, "Save Changes")

enter image description here

^ This will display a box with a Question mark icon but with ONLY an OK button.



MessageBox.Show("Do you wish to save changes?", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

enter image description here

^ This will display a box with Yes, No and Cancel buttons AND a Question mark icon.



As you can see, using MessageBox.Show enables you to have any buttons you want with any icon.

2
  • I just edited to add screenshots. I hope this helps.
    – RHDxSPAWNx
    Jun 13, 2014 at 18:47
  • 3
    Can also be done with MsgBox: MsgBox("Do you wish to save changes?", MsgBoxStyle.Question OR MsgBoxStyle.YesNoCancel, "Save Changes")
    – habakuk
    Oct 6, 2014 at 12:26
3

But the really nice thing about MsgBox is that it can be SystemModal e.g. If MsgBox("There is a new Quick Message!" & Environment.NewLine & "Do you want to read it now?", MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.SystemModal, "Quick Message") = MsgBoxResult.Yes Then...

I couldn't find a simple way of making If MessageBox.Show(... to be SystemModal.

My messages now get full prominence on screen. Yippee.

2

According to this site and the answers so far to my own question (see remark), as well my inability to display a specific help file using the msgbox function, I'd have to say use messagebox rather than msgbox if you want to show help. The msgbox function displays a help button, but apparently there is no way to put a helpfile in it! I'm showing the code I played around with below, and there is also a good code sample on the first link.

Imports Microsoft.visualbasic 'have to have this namespace to use msgbox
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Helpfilepath As String = "C:\Windows\Help\mui\0409\aclui.chm"
    Dim msgresult As Byte
    'BTW, Must use 0 for BLANK PARAMETER. Using messageboxoptions.defaultdesktoponly errors out with help btn.
    msgresult = MessageBox.Show("Text", "Messagebox", 0, _
            0, 0, 0, Helpfilepath)

    'displays help button, but how do you display the help file?
    msgresult = MsgBox("Text", MsgBoxStyle.MsgBoxHelp, "msgbox")
    'BTW, must use dialogresult rather than messageboxresult with windows forms
    If msgresult = DialogResult.Yes Then
        'etc
    End If
End Sub
End Class
1
0

The message box created using MsgBox() has a title of the form which created it, whereas the message box window created by MessageBox.Show() does not have any title.

1
  • 2
    It has a title: MessageBox.Show(message, title, button, icon)
    – habakuk
    Oct 6, 2014 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.