不能使用 System.Windows.Forms

我曾经尝试编写(我的第一个)一个 C # 程序:

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

这很好,但是如果我尝试使用 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();
}
}
}

我得到的错误是:

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

一些细节: - 我正在使用 Visual Studio 2012; - 我已经安装了.NET 开发工具包; 这是控制台应用。

也许是因为在控制台应用上不能使用系统,窗口,窗体? 如果是,应该是什么程序?我也尝试过使用表单,但是我只显示了一个窗口,没有代码。

288239 次浏览

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"

A console application does not automatically add a reference to System.Windows.Forms.dll.

Right-click your project in Solution Explorer and select Add reference... and then find System.Windows.Forms and add it.

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

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.

may be necesssary, unreference system.windows.forms and reference again.

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.

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.

For Those using Visual Studio 2022 with .Net Core 6.0

Sorry to revive this thread, but I created an account just to do so, as none of the solutions I found searching google for days worked for me alone, and seemed to only bring up only outdated tutorials.

Not Working =(

What DID work for me

  1. Double click your project (opening the csproj editor window)

Add the following lines (replacing the existing TargetFramework line):

<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>

My window, for referrence, looks like:

<Project Sdk="Microsoft.NET.Sdk">


<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>


</Project>
  1. Note Both lines! I read several solutions / videos that stopped simply after adding the "UseWindowsForms" tag, which did not solve the problem for me, even after unloading and reloading/closing and opening etc.

I stumbled on this solution after applying the "UseWindowsForms" tag, and then in desperation changing my NET framework in properties to 5.0, which caused it to work, and then noted when changing back to 6.0 it still worked.

If you forget the Framework tag in the future, you can reproduce the effect just by flipping your properties back and forth...

  1. Right click the project, and go to properties.

  2. Change the Target framework from .NET 6.0...to....NET 5.0

  3. Exit back to your code. Which after a few moments will show the System.Windows.Forms connecting properly.

  4. Open back up properties.

  5. Change the Target framework from .Net 5.0, back to .Net 6.0

Congratulations

You have (or at least I have) a .Net 6.0 project that is properly allowing me to use System.Windows.Forms (Including the Clipboard, which I suspect many here are looking for...).

I did due diligence on this, testing it multiple times across multiple projects, and it (at least for my setup) consistently works!

BTW. For those wondering. the [STAThread] Attribute tag seen in the pictures is needed to allow the Clipboard class to function. (this is also why I am not using top level statements in the example, but if you don't need that Class, the example works with top level statements (I needed it to show my Clipboard test....)

If you're using Visual Studio 2022 open the Project Properties and check the Windows.Forms - Enable Windows Forms for this Project setting.

enter image description here

For some reason, none of the solutions worked for me (Windows 11, VS 2022). I actually needed to use the "Windows Form app" template. See: https://learn.microsoft.com/en-us/visualstudio/ide/create-csharp-winform-visual-studio?view=vs-2022.