2012-03-10 36 views
46

我試圖讓(我第一次)C#程序:不能使用System.Windows.Forms的

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 Development Kit; - 它是一個控制檯應用程序。

也許是因爲在控制檯上應用程序不能使用System.Windows.Forms? 如果是這樣,程序應該是什麼?我也嘗試過一種形式,但我只是顯示一個窗口而沒有代碼。

+8

有時候錯誤信息會告訴你什麼是錯的:你缺少一個程序集引用。 [如何在Visual Studio中添加或刪除引用](http://msdn.microsoft.com/zh-cn/library/wkze6zky。aspx) – dtb 2012-03-10 13:11:56

+2

因爲這只是您的第一個程序,所以最好重新開始並創建Windows窗體應用程序。 VS2010項目嚮導將關注許多關於使用Windows窗體的數據集。 – Steve 2012-03-10 13:18:30

+6

dtb的評論的第一部分有點不爲人知。很明顯,提問者缺少一個程序集引用。問題是如何解決這個問題,因爲錯誤信息沒有澄清。 – jmort253 2013-02-28 23:46:01

回答

119

控制檯應用程序不會自動添加對System.Windows.Forms.dll的引用。

右鍵單擊解決方案資源管理器中的項目,然後選擇添加引用...然後找到System.Windows.Forms並添加它。

+0

好的,我在現有項目上遇到此問題。我認爲這個項目文件已經過時了,所以我從頭開始創建,覆蓋並添加了所有現有的類文件等。現在,當我去添加引用時,「System.Windows」和「System.Windows.Forms」沒有列在瀏覽對話框的「.Net」選項卡中。有任何想法嗎? – 2014-10-17 17:38:51

+1

@JayImerman它的組件標籤..對不起,幾年來不及。 – DCorrigan 2016-04-25 14:16:22

+0

一個非常明確的答案! – Ggalla1779 2017-03-27 12:22:19

15

您必須將名稱空間的引用添加到您的項目中,因爲由於某種原因它尚未添加,因此您可以從Visual Studio菜單中添加新建引用。

右鍵單擊「引用」▶「添加新引用」▶「System.Windows.Forms的」

0

確保Solution Explorer中可見 在MS Studio 2008中去查看,然後單擊解決方案資源管理

在Solution Explorer中去參考值使用參考 右鍵單擊並選擇Add Reference .. 選擇.NET選項卡 向下滾動直到你找到System.Drawing中 - >選擇它 - >點擊OK按鈕 執行相同的System.Windows.Forms的

當你運行你的形式,這將工作

(李雨陽VES在時間的某個地方)

1

只需添加引用對System.Windows.Forms.dll

0

轉到側面項目面板中,右鍵單擊引用 - >添加引用,找到System.Windows.Forms的

任何時候都會發生這樣的錯誤(您添加的某些命名空間丟失,顯然存在),解決方案可能是這樣的 - 添加一個引用。

這是需要的,因爲您的默認項目不包含所有內容,因爲您可能不需要它,因此可節省空間。一個好的做法是排除你不使用的東西。

0

可能是必需的, 重申system.windows.forms 並再次引用。

相關問題