1
class MyButton :System.Windows.Forms.Button
2
{
3
protected override bool IsInputKey(System.Windows.Forms.Keys keyData)
4
{
5
if (keyData == System.Windows.Forms.Keys.Left ||
6
keyData == System.Windows.Forms.Keys.Right)
7
return true;
8
return base.IsInputKey(keyData);
9
}
10
}
重寫之后就可以讓Button控件KeyDown事件中箭頭鍵響應了。
2



3

4



5

6

7

8

9

10

1
private void button1_KeyDown(object sender, KeyEventArgs e)
2
{
3
if(e.KeyCode == Keys.Left)
4
{
5
if (button1.Location.X >=2)
6
{
7
button1.Location = new Point(button1.Location.X - 2, button1.Location.Y) ;
8
}
9
}
10
if (e.KeyCode == Keys.Right)
11
{
12
if (button1.Location.X <= 500)
13
{
14
button1.Location = new Point(button1.Location.X + 2, button1.Location.Y);
15
}
16
}
17
}

2



3

4



5

6



7

8

9

10

11



12

13



14

15

16

17
