[英]'KeyPressEventArgs' does not contain defintion for KeyCode error
I'm trying to make a key press event in visual studio windows forms, but getting this error.我正在尝试在 Visual Studio Windows 窗体中创建按键事件,但出现此错误。 Any ideas?
有任何想法吗?
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode = Keys.E)
{
// The Code when key 'E' is pressed
}
}
I keep getting the error CS1061我不断收到错误 CS1061
Im trying to make a key press event in visual studio windows forms but getting this error:我试图在 Visual Studio Windows 窗体中创建一个按键事件,但收到此错误:
The error CS1061
CS1061 错误
This error is because KeyPressEventArgs
doesn't contain a definition for KeyCode
, please see Microsoft documents for more details on this compiler error.此错误是因为
KeyPressEventArgs
不包含KeyCode
的定义,有关此编译器错误的更多详细信息,请参阅 Microsoft 文档。 If you want to get the KeyCode
one way you can get it with a combination of KeyDown event and using KeyEventArgs .如果您想以一种方式获取
KeyCode
您可以通过KeyDown事件和使用KeyEventArgs的组合来获取它。
On another note, in your provided code you have if (e.KeyCode = Keys.E)
that line should give you an error of some sort along the lines of:另一方面,在您提供的代码中,
if (e.KeyCode = Keys.E)
该行应该给您一个if (e.KeyCode = Keys.E)
以下行的错误:
CS0200 Property or indexer ... cannot be assigned to -- it is read only
CS0200 属性或索引器 ... 无法分配给 -- 它是只读的
To fix this , you need to add two ==
so you can perform a equality check, not trying to assign.要解决这个问题,您需要添加两个
==
以便您可以执行相等性检查,而不是尝试分配。
Solution:解决方案:
To fix your main issue, please see below the new signature you can use.要解决您的主要问题,请参阅下面您可以使用的新签名。
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.E)
{
}
}
KeyPressEventArgs objects has only a definition for KeyChar, which type is char, if you want to easy compare that char with any value defined on Keys class you can rewrite that conditional like this: KeyPressEventArgs 对象只有 KeyChar 的定义,其类型是 char,如果您想轻松地将该 char 与 Keys 类上定义的任何值进行比较,您可以像这样重写该条件:
if ((int)e.KeyChar == (int)Keys.E)
{
// The Code when key 'E' is pressed
}
I hope it helps, and remember to use == in order to compare XD我希望它有帮助,记得使用 == 来比较 XD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.