按键事件的C#问题 [英] C# problem with keys down event

查看:67
本文介绍了按键事件的C#问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

This is my code:

private void Form2_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.Up)
  {
    picturebox1.Location = new Point(100, 100);
  }
}


我一点也没有错,由于某种原因,它什么也没做,只是不动.谁能帮忙,我做错了什么?


I''m getting no errors att all, and for some reason it''s not doing anything, it''s just not moving. Can anyone please help, what did I do wrong?

推荐答案

我认为一个问题是位置.想像一下您两次按下此键.在第二次单击时,picturebox1将与第一次单击后完全位于相同的位置,对吗?即使单击后也可能在同一位置.

第二个问题是表格.如果您在窗体上具有任何可焦点控制,则其中一个将抓住焦点(窗体无法获得焦点)并消耗键盘事件.要变通,您需要预览"表单上的事件.

另一种方法是使用Form.ProcessKeyCmd,您可以覆盖它.

请参阅属性Form.KeyPreview.首先,它可以是true.有关更多详细信息,请在此属性上单击F1并找到Microsoft示例代码-该示例代码将解释所有内容并显示如何解决您的问题.

—SA
I think one problem is location. Imaging you hit this key twice. On the second click picturebox1 will be exactly at the same location as after the first hit, right? It''s possible it was at the same location even after the click.

The second problem is the Form. If you have any focusable controls on the form, one of them will grab the focus (a Form cannot have the focus) and consume the keyboard events. To work around, you need "preview" the event on the form.

Another way is using Form.ProcessKeyCmd, you can override it.

Please see the property Form.KeyPreview. First thing, it can be true. For further detail, please hit F1 on this property and locate the Microsoft sample code — it will explain everything and show how to resolve your problem.

—SA


这篇关于按键事件的C#问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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