-2

I'm planning on doing a Jump and Run, so I need a moving pictureBox if I press W / A / S / D or something like that. I'm currently using the source code down there.

The Problem I have right now is, that I've got a lot of Buttons, other Picture Frames, text boxes etc in my current Form1 and if I press the button W now, my picture box won't get higher in coordinates because I think one of the other buttons etc is selected, does anybody know how to fix that? I tried the Source code in a new file without any other Buttons textboxes etc and it worked without any problems.

private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            pictureBox2.Top -= 100;
        }
    }

2 Answers2

2

You should set the KeyPreview property of the form to true, so you will get the key strokes before they reach the controls.

this.KeyPreview = true;

Read more here

Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
0

You need to set the KeyPreview of the Form to True in order to get the Form1_KeyDown to run when others objects in the Form are selected

AMINCHAR
  • 225
  • 1
  • 11