为什么此KeyPress代码不起作用? [英] Why doesn't this KeyPress code work?

查看:279
本文介绍了为什么此KeyPress代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C#编程的新手,我正在尝试制作一些测试应用程序.当您按D键时,将弹出一个消息框.但是,它似乎不起作用.请您看看是否可以找到问题并解决.

Hi,

I am new to C# programming and I''m trying to make a little test application. When you press the D key, a message box will pop up. However, it doesn''t seem to be working. Please may you see if you can find the problem and fix it.

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.D)
    {
        MessageBox.Show("You pressed D!");
    }

}



请记住要简单地告诉我这个问题,因为我真的还不太了解C#...

谢谢!

编辑:我尝试使用其他键(左)执行该代码,但仍无法正常工作...



Remember to please tell me the problem in simple terms because I really don''t understand C# too well yet...

Thanks!

EDIT: I''ve tried the code with a different key (Left) and it still fails to work correctly...

推荐答案



可能的原因是您的键盘焦点不在表单上.如果您在窗体上添加了更多控件,并且选项卡焦点位于某个控件上,则它将不会触发该事件.

如果要触发此事件,请确保当前焦点位于表单上.

希望你有主意.

祝您好运
Hi,

The possible cause is your keyboard focus is not on the form. if you have added more number of controls on your form and your tab focus is on some control then it will not fire the event.

If you want to fire this event, make sure your current focus is on the form.

Hope you got the idea.

Best luck


如果它是Windows应用程序,
然后检查表单的 KeyPreview 属性,该属性应为 True

和形式具有重点.

祝您编码愉快!
:)
if it is windows application,
then check KeyPreview property of your form it should be True

and form have focus.

Happy Coding!
:)


这篇关于为什么此KeyPress代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆