Hello To All!

I have started learning Vb.net. I am using Visual Studio Express 2012.
While making a console application, when I write "System.Windows.Forms" to use message box, then compiler gives an error: Class " System.Windows.Forms" is not defined. My compiler only supports two methods of this class
1-)Systems.Windows.Markup
2-) Systems.Windows.Input

but it is not accepting "Systems.Windows.Forms "

What is the solution of this problem?

Recommended Answers

All 13 Replies

using System.Windows.Forms;

namespace FormsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageBox.Show("I am a WinForms Message Box in a Console App");
        }
    }
}

@Dave that's a C# code, this question is on VB. The OP should make use of Console.Write method or try using MessageBox.Show() method to display message.

MrM - The code was just for pritaeas to show that you can use a message box in a console app - the language is not important.

I have already given the correct answer to this question which is to add a reference.

It is not common to use a messagbox in a console application, that's why the reference to Forms is not included by default.
If you would startup a Forms application, it will be there.

I agree with ddanbe, even though you can (thanks Dave) doesn't mean you should.

It is not common to use a messagbox in a console application, that's why the reference to Forms is not included by default.

A library project doesn't reference System.Windows.Forms by default either, but it's very common to define form components in a DLL. I'm not convinced the default references are a good argument for what you should and shouldn't use in a particular project type.

Console applications are somewhat rare relative to Forms, WPF, and library projects. However, it's not as uncommon as you might think to mix console and GUI stuff. A more realistic example than MessageBox would be OpenFileDialog or a custom form in a test project.

I can think of one good reason why you would want to use a messagebox in a console application. If you have an operation that runs for a long enough time that you don't want to watch it but you need to be notified as soon as it completes, you might want to pop up a notification in a messagebox. True, you could always BEEP, but you might want a notification that works even if the volume is muted.

I used to do a lot of system/network maintenance and I found this so useful that I wrote a stand-alone app, MessageBox.exe, that I could run in batch scripts for just that purpose.

Open a command prompt in Windows

Type "Regsvr32" then hit enter. If it's good enough for MS, it's good enough for me!

You'll need to add a reference to the System.Windows.Forms namespace (Project menu->Add reference...), then you'll be able to show a MessageBox from your console application.

Add this to the very top of your code:
Imports System.Windows.Forms

You should now be able to use MessageBox.Show()

Make sure to add References entry of System.Windows.Forms

Thre very first reply to this post was me telling the poster to add a reference.

No need to keep repeating that, it has already been answered correctly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.