using System.Windows.Forms - can't find namespace?

  • Thread starter Thread starter garyusenet
  • Start date Start date
G

garyusenet

I am using a C# solution that somebody kindly sent me.
It was a console application.

I am trying to get use of the MessageBox.

I have added the following using directive to the namespaces at the
start of the program : -

using System;
// I've added this...
using System.Windows.Forms;
//----------------------------------------------
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

However i get a compile error that the type of namespace windows does
not exist.
I have selected the properties of the project and changed the output to
console, but that didn't help.

Why am i getting that error?

Thanks,

Gary-
 
Hello,

You need to add a reference to System.Windows.Forms.dll to your project.
Right-click the References node in Solution Explorer in Visual Studio to
do that.


Oliver Sturm
 
I'm still pretty new to programming using C# and the .NET environment,
but to knowledge you can't simply add the System.Windows.Forms
namespace to a console application and be able to make use of the
WinForms controls.

I'm sure someone else can elaborate on this more.
-- Lance
 
Hello Lance,
I'm still pretty new to programming using C# and the .NET environment,
but to knowledge you can't simply add the System.Windows.Forms
namespace to a console application and be able to make use of the
WinForms controls.

I'm sure someone else can elaborate on this more.

Well, yes you can. What makes you think you can't? Try it!


Oliver Sturm
 
As I said I'm still very new and learning. You are right it is
possible, so I stand corrected.

Thanks,
-- Lance
 
Lance,

Not so fast. You may have been right the first time. There is a
critical piece of code that is automatically generated by VS when you
create windows project that is not created for a console project. All
windows controls and forms must be hosted on a thread with a message
loop for them to work correctly. A call to Application.Run will set
that up for you.

Brian
 
Hello Brian,

It seems to me that this is all becoming a matter of definition now - the
OP's question was clear and the answer pretty simple.

The general question "can I add a reference to System.Windows.Forms.dll to
a console application and use Windows Forms controls" must clearly be
answered "yes", if you ask me - for instance you can add said reference
and use a line like

MessageBox.Show("Hi");

in your console application without doing anything else, and a lot of more
complex things are also possible.

The question "what may be a list of things I need to do to fully convert
my existing console app to a Windows Forms one" wasn't asked here.
Obviously that list could be pretty long, depending on the existing
functionality implemented for the console app, and yes, it would most
probably contain the entry "add Application.Run(...)" somewhere in your
code.


Oliver Sturm
 
Back
Top