按钮更新新职位

本文关键字:新职位 更新 按钮 | 更新日期: 2023-09-27 18:21:22

我面临一个简单的问题,但让我感到困惑。

一些代码如下:

代码1

private void Main_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left)
        {
            label1.Location = new Point(label1.Location.X +10,label1.Location.Y + 10);
        }
    }

代码2

private void Main_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left)
        {
            label1.Location = new Point(label1.Location.X +10,label1.Location.Y + 10);
            button1.Location = new Point(button1.Location.X - 1,button1.Location.Y);
        }
    }

代码1运行良好,但一旦我在表单中添加了一个按钮和移动位置的代码,我的"KEY.Left"就不会返回任何信息(更新位置)。。。有人能给我解释一下吗??

按钮更新新职位

假设这是关于Windows窗体的:

  1. 您可以尝试将Form的KeyPreview属性设置为true,并处理Form的KeyUp事件

  1. 您可以在表单中覆盖ProcessCmdKey

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Left)
        {
            label1.Location = new Point(label1.Location.X + 10, label1.Location.Y + 10);
            button1.Location = new Point(button1.Location.X - 1, button1.Location.Y);
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }