欢迎来到 SO Tex,
我相信有两种方法可以实现这一点,只需添加:
选项 1:如果执行了 Enter 按键,则抓取下一个控件
在表单的属性中,设置按键预览形式的属性为true
.
下面的代码将捕获您的“Enter-Press”事件并执行您正在查找的逻辑:
private void [YourFormName]_KeyDown(object sender, KeyEventArgs e)
{
Control nextControl ;
//Checks if the Enter Key was Pressed
if (e.KeyCode == Keys.Enter)
{
//If so, it gets the next control and applies the focus to it
nextControl = GetNextControl(ActiveControl, !e.Shift);
if (nextControl == null)
{
nextControl = GetNextControl(null, true);
}
nextControl.Focus();
//Finally - it suppresses the Enter Key
e.SuppressKeyPress = true;
}
}
这实际上允许用户按“Shift+Enter”也可以转到正在进行的选项卡。
选项 2:使用SendKeys http://msdn.microsoft.com/en-us/library/8c6yea83%28v=vs.85%29.aspx method
private void [YourFormName]_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
我不确定这种方法是否仍然常用或可能被视为“黑客”?我会推荐第一个,但我相信两者都应该满足您的需求。