C# 不能使用 System.Windows.Forms
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9646684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use System.Windows.Forms
提问by Ramy Al Zuhouri
I have tried making (my first) a C# program:
我曾尝试制作(我的第一个)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();
}
}
}
This goes well, but if I try using System.Windows.Forms:
这很顺利,但如果我尝试使用 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.
一些细节: - 我使用的是 Visual Studio 2012;- 我已经安装了 .NET 开发工具包;- 这是一个控制台应用程序。
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.
也许是因为在控制台应用程序不能使用 System.Windows.Forms?如果是这样,应该是什么程序?我也尝试过使用表单,但我只显示一个窗口而没有显示代码。
采纳答案by Kendall Frey
A console application does not automatically add a reference to System.Windows.Forms.dll.
控制台应用程序不会自动添加对 System.Windows.Forms.dll 的引用。
Right-click your project in Solution Explorer and select Add reference... and then find System.Windows.Forms and add it.
在解决方案资源管理器中右键单击您的项目并选择添加引用...,然后找到 System.Windows.Forms 并添加它。
回答by aleroot
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.
您必须将命名空间的引用:System.Windows.Forms 添加到您的项目中,因为由于某种原因它尚未添加,因此您可以从 Visual Studio 菜单添加新引用。
Right click on "Reference" ? "Add New Reference" ? "System.Windows.Forms"
右键单击“参考”?“添加新参考”?“系统.Windows.Forms”
回答by eyesonly
Ensure Solution Explorer is visible In MS Studio 2008 Go to view and click Solution explorer
确保解决方案资源管理器在 MS Studio 2008 中可见 转到查看并单击解决方案资源管理器
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
在解决方案资源管理器中转到引用右键单击引用并选择添加引用.. 选择 .NET 选项卡向下滚动直到找到 System.Drawing -> 选择它 -> 单击确定按钮 对 System.Windows.Forms 执行相同操作
When you run your form this will work
当您运行表单时,这将起作用
(eddie lives somewhere in time)
(埃迪住在某个时间的某个地方)
回答by Moayad Myro
just add reference to System.Windows.Forms.dll
只需添加对 System.Windows.Forms.dll 的引用
回答by Bojidar Stanchev
go to the side project panel, right click on references -> add reference and find System.Windows.Forms
转到侧面项目面板,右键单击引用 -> 添加引用并找到 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.
这是必需的,因为您的默认项目不包含所有内容,因为您可能不需要它,因此可以节省空间。一个好的做法是排除您不使用的东西。
回答by R.Alonso
may be necesssary, unreference system.windows.forms and reference again.
可能是必要的,取消引用 system.windows.forms 并再次引用。
回答by jorge
To add the reference to "System.Windows.Forms", it seems to be a little different for Visual Studio Community 2017.
要添加对“System.Windows.Forms”的引用,Visual Studio Community 2017 似乎有点不同。
1) Go to solution explorer and select references
1)转到解决方案资源管理器并选择参考
2) Right-click and select Add references

3) In Assemblies, check System.Windows.Forms and press ok
3) 在程序集中,检查 System.Windows.Forms 并按确定
4) That's it.
4)就是这样。

