I have tried making (my first) a C# program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
Console.ReadLine();
}
}
}
This goes well, but if I try using System.Windows.Forms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
System.MessageBox("hello");
Console.ReadLine();
}
}
}
This is the error I get:
Error 1 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\Ramy\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 5 14 ConsoleApplication1
Some details: - I am using Visual Studio 2012; - I have installed the .NET Development Kit; - It is a Console Application.
Maybe it's because on a Console Application can't use System.Windows.Forms? If so, what program should be? I also have tried with a form, but I was only displaying a window and no code.
This question is related to
c#
winforms
visual-studio
go to the side project panel, right click on references -> add reference and find System.Windows.Forms
Any time some error like this occurs (some namespace you added is missing that is obviously there) the solution is probably this - adding a reference.
This is needed because your default project does not include everything because you probably wont need it so it saves space. A good practice is to exclude things you're not using.
Adding System.Windows.Forms reference requires .NET Framework project type:
I was using .NET Core project type. This project type doesn't allow us to add assemblies into its project references. I had to move to .NET Framework project type before adding System.Windows.Forms
assembly to my references as described in Kendall Frey answer.
Note: There is reference System_Windows_Forms
available under COM tab (for both .NET Core and .NET Framework). It is not the right one. It has to be System.Windows.Forms
under Assemblies tab.
You have to add the reference of the namespace : System.Windows.Forms to your project, because for some reason it is not already added, so you can add New Reference from Visual Studio menu.
Right click on "Reference" ? "Add New Reference" ? "System.Windows.Forms"
may be necesssary, unreference system.windows.forms and reference again.
Ensure Solution Explorer is visible In MS Studio 2008 Go to view and click Solution explorer
In Solution explorer go to Reference Right click on Reference and select Add Reference.. Select .NET tab Scroll down till you find System.Drawing -> select it -> click on OK button Do the same for System.Windows.Forms
When you run your form this will work
(eddie lives somewhere in time)
just add reference to System.Windows.Forms.dll
Source: Stackoverflow.com