假设你有3个文本框需要进行快捷键设置,
1 向用户说明快捷键
假设label1对应的输入控件的快捷键为alt+1
this.label1.Text = "my test(&1) ";
&符号会将后随的字符加下划线显示
2 将这些框的KeyDown 事件连接到textBox1_KeyDown函数
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
this.textBox3.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
3 编写按键处理程序
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt) // 当按下Alt时的处理
{
switch (e.KeyCode)
{
case Keys.D1: // 如果同时为数字0
textBox1.Focus();
e.Handled = true;
break;
case Keys.D2:
textBox2.Focus();
e.Handled = true;
break;
case Keys.D3:
textBox3.Focus();
e.Handled = true;
break;
default:
break;
}
}
}
阅读(1404) | 评论(0) | 转发(0) |