2013-12-19 336 views
0

這是我的代碼。C#命名空間'System.Windows'中不存在類型或命名空間名稱'Forms'

namespace TextRPG1 
{ 
    public class Program 
    { 
     private bool nonyn = false; 

     static void Main(string[] args) 
     { 
     //Introduction 
     Console.WriteLine("Welcome to TextRPG By AgentMcBride."); 
     Console.WriteLine("You can exit at any time by hitting the ESC key!"); 
     Console.WriteLine("Press any key to begin!"); 
     Console.ReadKey(); 
     Console.WriteLine("Great! Lets begin!"); 

     //Backstory 
     Console.WriteLine(" BACKSTORY: You are a peasant trying to work his way through the castle to save the princess."); 

     //Inventory tutorial 
     Console.WriteLine("INVENTORY: You will pick up items along the way to help you save the princess. When encountering an item, the program will ask you y or n. Type y to collect the item. Note: You can only store 5 items at a time in your sack!"); 
     Console.WriteLine("Lets try picking up an item!"); 
     Console.WriteLine("You have found an apple! Would you like to put it in your sack? Type y or n!"); 

     //Handle the KeyDown event to determine the type of character entered into the control. 
     private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
     { 
      // Initialize the flag to false. 
      nonyn = false; 

      // Determine whether the keystroke is a number from the top of the keyboard. 
      if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) 
      { 
       // Determine whether the keystroke is a number from the keypad. 
       if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) 
       { 
        // Determine whether the keystroke is a backspace. 
        if(e.KeyCode != Keys.Back) 
        { 
        // A non-numerical keystroke was pressed. 
        // Set the flag to true and evaluate in KeyPress event. 
        nonyn = true; 
        } 
       } 
      } 
      //If shift key was pressed, it's not a number. 
      if (Control.ModifierKeys == Keys.Shift) 
      { 
       nonyn = true; 
      } 
     } 

     // This event occurs after the KeyDown event and can be used to prevent 
     // characters from entering the control. 
     private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
     { 
      // Check for the flag being set in the KeyDown event. 
      if (nonyn == true) 
      { 
       // Stop the character from being entered into the control since it is non-numerical. 
       e.Handled = true; 
      } 
     } 
     } 
    } 
} 

它不斷給我的錯誤

「的類型或命名空間名稱‘形式’並不在命名空間存在‘System.Windows’」

在那裏說System.Windows.Forms的.KeyEventArgs

此外,在當前上下文中找不到名稱「key」和「control」。

即時通訊編程新手,請不要討厭!

+3

你確定這是一個Windows窗體應用程序? – PoweredByOrange

+1

'命名空間'System.Windows'中不存在類型或命名空間名稱'Forms' - 這是正確的。 'Forms'類存在於'System.Windows.Forms'中。 – Brian

回答

3

這似乎是一個控制檯應用程序,它看起來像你試圖確定什麼鍵被按下。問題在於,您使用的是表單應用程序中的按鍵事件,請查看Console.ReadKey()的文檔,它可能會幫助您找到所需的內容。

http://msdn.microsoft.com/en-us/library/471w8d85(v=vs.110).aspx

編輯

這裏是你如何使用它一個例子:

var key = Console.ReadKey(); 

    if (key.KeyChar.ToString() == "y") 
    { 
     Console.WriteLine("-- Yes!"); 
     Console.ReadKey(); 
    } 
    else if (key.KeyChar.ToString() == "n") 
    { 
     Console.WriteLine("-- No!"); 
     Console.ReadKey(); 
    } 
    else 
    { 
     Console.WriteLine("-- That isn't an option!"); 
     Console.ReadKey(); 
    } 
0

這裏最可能的原因是您沒有添加對System.Windows.Forms.dll組件的引用。爲了做到這一點做以下

  • 右鍵單擊該項目並選擇「添加引用」
  • 轉到框架部分
  • 選擇System.Windows.Forms的
相關問題