简体   繁体   English

“KeyPressEventArgs”不包含 KeyCode 错误的定义

[英]'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.

相关问题 获取“ system.windows.forms.keypresseventargs”不包含“ keycode”的定义” - Getting “system.windows.forms.keypresseventargs' does not contain a definition for 'keycode'” IServiceCollection不包含AddHttpClient的定义 - IServiceCollection does not contain a defintion for AddHttpClient IGrouping <decimal,string> 不包含“名称”的定义 - IGrouping <decimal,string> does not contain a defintion for 'name' Visual Studio 2015存储库模式不包含以下定义 - Visual Studio 2015 repository pattern does not contain defintion for System.Web.WebPages.Html.HtmlHelper不包含定义 - System.Web.WebPages.Html.HtmlHelper does not contain a defintion C# “KeyCode”不包含“numberKey”的定义 - C# 'KeyCode' does not contain a definition for 'numberKey' UIKIt.UIImage不包含Save的定义,也没有找到UIKit.UIImage类型的扩展方法保存。您是否正在查看程序集引用? - UIKIt.UIImage does not contain a defintion for Save and no extension method save of type UIKit.UIImage not found.Are you mIssing assembly reference? IObservable不包含定义,但实际上那里有定义 - IObservable doesnt Contain the defintion, but actualy there is the definition in there KeyPressEventArgs的问题 - Problems with KeyPressEventArgs 错误:“ keyCode”为null或不是对象 - Error: 'keyCode' is null or not an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM