Share via


Why is MessageBox undefined after importing System.Windows.Forms?

Question

Friday, November 25, 2005 3:45 AM

I am new to Visual Studio.  I have VS 2005 Beta 2 installed.  I have a simple exercise from a book which Imports System.Windows.Forms at the top and then calls MessageBox to display results.  Here is a code snippet:

Imports System.Windows.Forms

Module Module1
    Sub Main()
        Dim amount, principal As Decimal
        Dim rate As Double
        Dim year As Integer
        Dim output As String

        principal = 1000.0
        rate = 0.05
        output = "Year" & vbTab & "Amount on deposit" & vbCrLf

        For year = 1 To 10
            amount = principal * (1 + rate) ^ year
            output &= year & vbTab & _
                String.Format("{0:C}", amount) & vbCrLf
        Next

        MessageBox.Show(output, "Compound Interest", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Module

When I compile the program I see the error "Name 'MessageBox' is not declared" as well as similar errors for MessageBoxButtons and MessageBoxIcon.  When I look in the object browser for my project under System.Windows.Form I do not see any MessageBox classes only WebBrowserPermission, WebBrowserPermissionAttribute, WebBrowserPermissionLevel.  When I browse objects for .NET framework, the MessageBox classes appear as well as numerous others.

Has anyone seen this before?  Is there a problem with my Visual Studio installation? 

Thx
Frank

All replies (3)

Friday, November 25, 2005 4:35 AM âś…Answered | 3 votes

Yes, you've pulled the namespace into scope, but you're assuming the dll is being imported.  If you right click on the project name and choose 'Add reference', scroll down and double click on 'System.Windows.Forms'.

Now it will work.

Imports is misleading, it doesn't import anything, it simply tells VB.NET that a namespace that exists in your project should be regarded as in scope, so you don't need to type it in to access objects in it.  Without the step I described, your project does not have any objects with that namespace, you'll find that the imports statement is underlined as well, but the error window doesn't say anything about it.


Friday, November 25, 2005 4:00 AM

Do you have a reference to the System.Windows.Forms dll in your project ?


Friday, November 25, 2005 4:29 AM

Thanks for your quick reply. 

In my Module1.vb I have the "Imports System.Windows.Forms" statement at the beginning of the code.  Do I need an additional reference to the dll in the VB module or elsewhere in the project?

Frank Leonardi