Reputation: 55
I want to move a button using a function which will be activated through a keyboard button, but I can't seem to make it accept my input. If I try to run the function through a button press, it works fine, so I know the function is not to blame. What am I doing wrong that it's not accepting my keyboard input?
private void MoveLeft()
{
_y = btnBot.Location.Y;
_x = btnBot.Location.X;
btnBot.Location = new System.Drawing.Point(_x - 10,_y);
}
void MoveLeft_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MoveLeft();
}
}
Upvotes: 0
Views: 209
Reputation: 438
To ensure that your form key handle event is triggered, even if another control has focus, make sure the KeyPreview property is set to true.
In your main form, add the following line, or set it during design time.
this.KeyPreview = true;
Upvotes: 2