我有几个文本框,希望按下回车键时可以像按下Tab键一样切换到下一个文本框。请问如何实现这种功能,而且不需要在文本框类中添加任何代码(如果可能的话,不要使用覆盖等方法)?
我有几个文本框,希望按下回车键时可以像按下Tab键一样切换到下一个文本框。请问如何实现这种功能,而且不需要在文本框类中添加任何代码(如果可能的话,不要使用覆盖等方法)?
这是我通常使用的代码。 必须在 KeyDown 事件中使用。
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
更新
另一种方法是发送 "TAB" 键!并覆盖该方法,使其更加容易 :)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter))
{
SendKeys.Send("{TAB}");
}
return base.ProcessCmdKey(ref msg, keyData);
}
SelectNextControl
比 GetNextControl
更好,因为它会自动跳过 Label、Panel、GroupBox、PictureBox、ProgressBar、Splitter 和 LinkLabel。更多信息请参见 http://msdn.microsoft.com/en-us/library/system.windows.forms.control.selectnextcontrol.aspx - Christian JunkSelectNextControl
使得 Enter 键的行为更像是 Tab 键,如果第三个参数设置为 true
,它还会跳过 TabStop
设置为 false
的控件。 - Nikola Novak你可以在任何控件的keyDown事件中编写代码:
if (e.KeyCode == Keys.Enter)
{
if (this.GetNextControl(ActiveControl, true) != null)
{
e.Handled = true;
this.GetNextControl(ActiveControl, true).Focus();
}
}
GetNextControl 在 Vista 上无法正常工作。
为了在 Vista 上使其正常工作,您需要使用下面的代码替换 this.GetNextControl...:
System.Windows.Forms.SendKeys.Send("{TAB}");
你不需要创建一个“回车事件处理程序”
你只需要创建一个“中央”的KeyDown事件:
示例
private void General_KeyDown(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (this.GetNextControl(ActiveControl, true) != null)
{
e.Handled = true;
this.GetNextControl(ActiveControl, true).Focus();
}
}
}
e.Handled = true
并不总是消除噪音/声音/铃声的答案。e.Handled = e.SuppressKeyPress = true;
e.SuppressKeyPress
。这对我起了作用
if (e.Key == Key.Enter)
((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));
TextBox
。相反,将其转换为UIElement
,然后您可以在每个控件上使用此方法。同时,您应该提到这仅适用于WPF而不是Winforms。 - Oliver Private Sub frmStart_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
System.Windows.Forms.SendKeys.Send("{TAB}")
End If
End Sub
我在一个文本框的keydown事件中使用了这段代码。
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
我无法处理表单中所有文本框的keydown
事件。请提供建议。谢谢。
private void alltextBoxes_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
}
catch
{
}
}
private void lastinput_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
System.Windows.Forms.SendKeys.Send("{TAB}");
}
}
catch
{
}
}
这样更好,因为当你按下回车键时,可以聚焦到下一个选项卡。你需要先设置选项卡顺序。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter))
{
SelectNextControl(ActiveControl, true, true, true, true);
}
return base.ProcessCmdKey(ref msg, keyData);
}
对于那些使用VB编程的人...
Public Class NoReturnTextBox
Inherits System.Windows.Forms.TextBox
Const CARRIAGE_RETURN As Char = Chr(13)
' Trap for return key....
Private Sub NoReturnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = CARRIAGE_RETURN Then
e.Handled = True
System.Windows.Forms.SendKeys.Send(vbTab)
End If
End Sub
End Class