C#,对吗?代码卡在一个地方? [英] C#, huh? Code Stuck In One Place?

查看:59
本文介绍了C#,对吗?代码卡在一个地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码

I have this code

private void Form2_Keydown(object sender, KeyEventArgs e)
  {
      if (e.KeyCode == Keys.Up)
      {
          You.Location = new Point(You.Location.X, You.Location.Y - 5);
      }
  }


(您=图片框)

而且我没有出现任何错误,它可以在Form1中很好地工作,但是当我尝试在Form2中使用它时,什么也没发生,我有点困惑,有人可以帮助我吗?


(You = Picturebox)

And I get no errors popping up on it,and it works in Form1 great but when I try to use it in Form2 Nothing happens and I''m a bit confused can anyone help me?

推荐答案

您确定表单上的"KeyPreview"属性设置为True吗?
Have you made sure the ''KeyPreview'' property on the form is set to True?


重复的问题!我已经回答:

按下事件的C#问题 [
Duplicate question! I already answered:

C# problem with keys down event[^].

I think you ignored second part of the Answer.

Please try and tell us what you get. You can also use advice about ProcessKeyCmd by Dylan, should work.

—SA


Sunasara是正确的:您是否忘记将KeyDown事件分配给处理程序?您可以在设计器模式下执行此操作,也可以直接在构造函数中执行此操作,例如:
Sunasara is right: didn''t you forget to assign the KeyDown event to your handler? You can do this in designer mode, or directly in the constructor for example:
public Form2()
{
    InitializeComponent();
    KeyDown += new KeyEventHandler(Form2_KeyDown);
}


这篇关于C#,对吗?代码卡在一个地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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