[c#] Can't use System.Windows.Forms

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

The answer is


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)


To add the reference to "System.Windows.Forms", it seems to be a little different for Visual Studio Community 2017.

1) Go to solution explorer and select references

enter image description here

2) Right-click and select Add references enter image description here

3) In Assemblies, check System.Windows.Forms and press ok

enter image description here

4) That's it.


just add reference to System.Windows.Forms.dll


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references