大家好,欢迎来到IT知识分享网。
某些键,如 Tab、Return、Esc 和箭头键,由控件自动处理。为使这些键引发 KeyDown 事件,必须在窗体上的每个控件中重写 IsInputKey 方法。用于重写 IsInputKey 的代码需要确定是否按下了某个特殊键,并且需要返回一个 true 值。
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
}
class
MyButton :System.Windows.Forms.Button
2
{
3
4
5
6
7
8
9
10
重写之后就可以让Button控件KeyDown事件中箭头键响应了。
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
}
private
void
button1_KeyDown(
object
sender, KeyEventArgs e)
2
{
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/15144.html