gpt4 book ai didi

C#命名空间 'Forms'中不存在类型或命名空间名称 'System.Windows'

转载 作者:行者123 更新时间:2023-12-04 16:59:23 31 4
gpt4 key购买 nike

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

7年前关闭。




Improve this question




这是我的代码。

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’中不存在类型或命名空间名称‘Forms’”

它说 System.Windows.Forms.KeyEventArgs

在当前上下文中也找不到名称“key”和“control”。

我是编程新手,所以请不要讨厌!

最佳答案

这似乎是一个控制台应用程序,看起来您正在尝试确定按下了哪些键。问题是您正在使用表单应用程序中的按键事件,请查看 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();
}

关于C#命名空间 'Forms'中不存在类型或命名空间名称 'System.Windows',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693265/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com