0

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?

9
Contributors
13
Replies
58
Views
2 Years
Discussion Span
Last Post by DaveAmour
Featured Replies
  • 1
    ddanbe 2,654   2 Years Ago

    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. Read More

0
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");
        }
    }
}
0

@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.

0

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.

1

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.

0

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.

0

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.

1

Open a command prompt in Windows

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

0

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()

Edited by mridul.ahuja

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.