如何使TextBox上的Enter作为TAB按钮
我有几个文本框。 我想让Enter按钮充当Tab。 因此,当我将在一个文本框中时,按Enter将使我移动到下一个文本框。 你能告诉我如何实现这种方法而不在textbox类中添加任何代码(如果可能的话,没有覆盖等等)?
这是我经常使用的代码。 它必须是KeyDown事件。
if (e.KeyData == Keys.Enter) { e.SuppressKeyPress = true; SelectNextControl(ActiveControl, true, true, true, true); }
UPDATE
其他方式是发送“TAB”键! 并覆盖该方法使它更容易:)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.Enter)) { SendKeys.Send("{TAB}"); } return base.ProcessCmdKey(ref msg, keyData); }
你可以写任何控件的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(); } } }
然后你要做的就是去设计师选择你想用EnterKey循环的所有文本框(通过按住Ctrl并用鼠标点击文本框来选择它们)然后转到事件(雷声像按钮),搜索Keydown事件和类型在General_KeyDown里面。 现在你所选择的所有文本框都将具有相同的keydown事件:)这使得所有内容都变得更加容易,因此想象一个包含100个文本框的表单,并且您希望使用enter循环遍历….为每个texbox创建一个单独的事件是…好吧,不是一个制作节目的正确方法,它不是很整洁。 希望它有所帮助!!
大段引用
重要的是要注意,如果每次控件期望相关的按钮控制并且e.Handled = true
时,你会得到恼人的“叮”声或警告声并不总是能够摆脱噪音/声音的答案/丁。
如果控件(即单行文本框)期望相关按钮“输入”您的条目,那么您还必须处理“缺失”控件。
e.Handled = e.SuppressKeyPress = true;
当摆脱’叮当’的声音时,这可能会有所帮助。
在我的情况下,我的用户需要使用“ENTER KEY”,因为我们正在从终端/绿屏应用程序转换到Windows应用程序,他们更习惯于通过字段而不是标签来“输入” 。
所有这些方法都有效但在我添加e.SuppressKeyPress
之前仍然保持令人讨厌的声音。
这对我有用
if (e.Key == Key.Enter) ((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));
如果您定义所有控件的Tab Order并使Form.KeyPreview = True,则只需要:
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
对于那些在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
我在其中一个文本框keydown事件中使用此代码
if (e.KeyData == Keys.Enter) { e.SuppressKeyPress = true; SelectNextControl(ActiveControl, true, true, true, true); }
无法处理表单中所有文本框的keydown
事件。 建议一些事情。 谢谢
这是我用于VB.NET的解决方案
-
在表单属性中设置Keypreview = True 。
-
将此代码放在表单keydown事件中:
如果(e.KeyData = Keys.Enter)那么
‘对于任何多行控件,你必须退出让多行’文本框介绍’按键才能使行跳过。
If ActiveControl.Name = txtMyMutilineTextBox.Name Then Exit Sub e.SuppressKeyPress = True SelectNextControl(ActiveControl, True, True, True, True)
万一
请享用 !!!!
Xabier Aberasturi Larruzea
猜测一下:
// on enter event handler parentForm.GetNextControl().Focus();
我会把Pharabus和arul的回答结合起来:
private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\r') { e.Handled = true; parentForm.GetNextControl().Focus() } }
如果这有帮助,请告诉我! JFV