且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#windows应用程序按键事件问题

更新时间:2022-02-13 08:08:36

只需检查你的if语句..你实际上正在分配一个值..use ==而不是。

简单的错误。





希望它有所帮助!!
just check your if statement..you are actually assigning a value..use == instead.
simple mistake.


hope it helps!!


你需要将密钥转换为char并需要给==而不是=。



试试这个,

You need to cast the key to char and need to give == instead of =.

Try this,
if (e.KeyChar == (char)Keys.Enter)
{
//logic here

}


如果要比较相等性,请使用等于运算符 ==



Use the equality operator == if you want to compare for equality.

private void txtRollNumber_KeyPress(object sender, KeyPressEventArgs e)
{
 if (e.KeyChar == (char)Keys.Enter)
  {
  //Do your operation here
  }
}





现在它应该可以工作!



编辑:添加(char)将其强制转换为char类型。



Now its should work!

Edit : Added (char) to cast it to char type.