Jan 26 2005

VB.NET: MsgBox() or MessageBox.Show()?

Published by gringod at 2:41 pm under .NET

I’m currently using VB.NET as my main language because thats what my project at work requires. But having come from programming in VB6 I was just wondering if I’m using some of the old VB6 constructs and functions just because they are still there or because they really are the best way of doing things in VB.NET.

I think the best way to demonstrate what I mean is the MsgBox() function. This function takes several parameters are displays a little message box to the user with one or more buttons for them to press to indicate their response. As far as I am aware (but I may be wrong), the MsgBox function was the only way to do this in VB6 without writing your own version of the message box.

In VB.NET, however, there is now the MessageBox class in the System.Windows.Forms namespace. Out of habbit, though, I am still using the MsgBox function simple because its there. MsgBox is actually in the Microsoft.VisualBasic namespace which gets automatically imported into a VB.NET project.

But which is better: the MsgBox() function, or the MessageBox class? There’s only one way to find out… use Lutz Roeders .NET Reflector tool to take a look at the innards of both of these.

When you have a look inside it is easy to see which method should be used to display a message box. It appears that Microsoft.VisualBasic.MsgBox() does some exception check and then passes the call onto System.Windows.Forms.MessageBox.Show() this then does pretty much the same exception checking and the passes the call onto System.Windows.Forms.SafeNativeMethods.MessageBox() which is itself a wrapper for a win32 API call to user32.dll.

So which is the best method to show a message box? Between Microsoft.VisualBasic.MsgBox() and System.Windows.Forms.MessageBox.Show() you should really use the latter, System.Windows.Forms.MessageBox. Firstly it removes an unnecessary layer of exception checking and secondly using the Microsoft.VisualBasic namespace will reduce your ability to port your application to another platform/language.

5 Responses to “VB.NET: MsgBox() or MessageBox.Show()?”

  1. GrinGodon 18 Feb 2005 at 10:29 am

    If you want to test displaying message boxes without having to create a whole VB.NET project then try my VB.NET CodeTester

  2. amiton 05 Aug 2006 at 7:26 pm

    MCA

  3. elangoon 21 Dec 2006 at 11:43 am

    how can i create a message box in vb.net (web application). in that message box how can i add it as a component.

  4. gringodon 21 Dec 2006 at 12:25 pm

    Hi Elango,

    First up I must ask why you are wanting to create your own message box control? If it is to just make it appear different to the standard windows one (but not work differently) then I would beg you to use the standard windows message box. It really winds me up when developers change interface standards just for the sake of “being different”.

    If, however you need a message box that requires functionality that is not provided by the standard windows message box then you may want to head over to the Code Project. Two articles you may find usefull are: XMessageBox - a reverse engineered MessageBox() and Dissecting the MessageBox.

    The second article is in C# but don’t panic. If your not a C# coder it would be worth your while learning to read C# even if you don’t code it. Its actually very similar to VB.Net, it just uses {} instead of End X.

  5. Pwhndvveon 09 Aug 2008 at 5:43 pm

    Thousands and look closer buy cytotec meat steamed held.

Trackback URI | Comments RSS

Leave a Reply