Key Events for Printing in C # - Moving PictureBox

I am trying to move a PictureBox (picUser) up and down using keypress events. I am new to C # and can do this via VB. Therefore, I am confused by the fact that the problem is in the following code:

    private void picUser_keyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            picUser.Top -= 10;
        }
    }

There is no “error” in the code, the picture simply does not move.

+3
source share
2 answers

A PictureBoxhas no event KeyDown. Instead, he has PreviewKeyDownand requires that he has PictureBoxfocus.

I would suggest using the KeyDown formone that is hosted PictureBoxand using the exact same code:

public Form1()
{
     InitializeComponent();
     this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
     if (e.KeyCode == Keys.W)
     {
         picUser.Top -= 10;
     }
}
+6
source

, , picUser , , .

picUser , . , , , KeyPreview true, keyDown ( e.Handled = true, , , ).

+2

All Articles