C# 如何使 TextBox 上的 Enter 充当 TAB 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/371329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Enter on a TextBox act as TAB button
提问by Tomasz Smykowski
I've several textboxes. I would like to make the Enter button act as Tab. So that when I will be in one textbox, pressing Enter will move me to the next one. Could you please tell me how to implement this approach without adding any code inside textbox class (no override and so on if possible)?
我有几个文本框。我想让 Enter 按钮充当 Tab。因此,当我在一个文本框中时,按 Enter 会将我移到下一个。你能告诉我如何在不添加任何代码的情况下在文本框类中实现这种方法(如果可能,不要覆盖等等)?
采纳答案by Behzad
Here is the code that I usually use. It must be on KeyDown event.
这是我通常使用的代码。它必须在 KeyDown 事件上。
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
UPDATE
更新
Other way is sending "TAB" key! And overriding the method make it so easier :)
另一种方式是发送“TAB”键!并覆盖该方法使其更容易:)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter))
{
SendKeys.Send("{TAB}");
}
return base.ProcessCmdKey(ref msg, keyData);
}
回答by arul
Taking a wild guess:
大胆猜测:
// on enter event handler
parentForm.GetNextControl().Focus();
回答by JFV
I would combine what Pharabus and arul answered like this:
我会结合 Pharabus 和 arul 的回答如下:
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ‘\r')
{
e.Handled = true;
parentForm.GetNextControl().Focus()
}
}
Let me know if this helps! JFV
让我知道这是否有帮助!JFV
回答by Patrick Desjardins
You can write on the keyDownof any control:
你可以在任何控件的keyDown上写:
if (e.KeyCode == Keys.Enter)
{
if (this.GetNextControl(ActiveControl, true) != null)
{
e.Handled = true;
this.GetNextControl(ActiveControl, true).Focus();
}
}
GetNextControl doesn't work on Vista.
GetNextControl 在Vista 上不起作用。
To make it work with Vista you will need to use the code below to replace the this.GetNextControl...:
要使其与 Vista 配合使用,您需要使用以下代码替换 this.GetNextControl...:
System.Windows.Forms.SendKeys.Send("{TAB}");
回答by Patrick Desjardins
You don't need to make an "enter event handler"
您不需要制作“输入事件处理程序”
All you need to do is make a "central" KeyDown event:
您需要做的就是创建一个“中心” KeyDown 事件:
example
例子
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();
}
}
}
Then all you have to do is go to designer select all textboxes you wish to cycle through with EnterKey (select them by holding down Ctrl and clicking on textbox with the mouse) then go to Events(thunder like button), search Keydown event and type inside General_KeyDown. Now all your selected Textboxes will have the same keydown event :) This makes everything muuuuch much easier, cause imagine a form with 100 textboxes and you want to cycle through all with enter.... making an apart event for each texbox is... well not a proper way to make a program, it ain't neat. Hope it helped!!
然后你所要做的就是去设计器选择你想用 EnterKey 循环的所有文本框(通过按住 Ctrl 并用鼠标点击文本框来选择它们)然后去事件(像雷一样的按钮),搜索 Keydown 事件并输入在 General_KeyDown 内。现在您选择的所有文本框都将具有相同的 keydown 事件 :) 这使一切变得更加容易,因为想象一个具有 100 个文本框的表单,并且您想使用 enter 循环浏览所有文本框.... 为每个文本框制作一个单独的事件是.. . 好吧,这不是制作程序的正确方法,它不整洁。希望有帮助!!
Blockquote
块引用
回答by Mac
For those of you that code in vb...
对于那些在 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
回答by Cadair Idris
This worked for me
这对我有用
if (e.Key == Key.Enter)
((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));
回答by FXX
If you define Tab Order of all controls and make Form.KeyPreview = True, only need this:
如果定义所有控件的 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
回答by Greg
It is important to note that if you will get an annoying "ding" or warning sound each time that the control is expecting an associated button control and e.Handled = true
isn't always the answer to rid yourself of the noise/sound/ding.
重要的是要注意,如果每次控件需要关联的按钮控件时都会听到烦人的“叮”声或警告声,并且e.Handled = true
并不总是消除噪音/声音/叮当声的答案。
If the control (i.e. single-line textbox) is expecting an associated button to 'ENTER' your entry, then you must also deal with the 'missing' control.
如果控件(即单行文本框)需要一个关联的按钮来“输入”您的条目,那么您还必须处理“丢失”的控件。
e.Handled = e.SuppressKeyPress = true;
This may be helpful when getting rid of the 'ding' sound.
这在消除“叮”声时可能会有所帮助。
For what it's worth- in my circumstance my users needed to use the "ENTER KEY" as we were transitioning from a terminal/green-screen application to a windows app and they were more used to "ENTER-ing" through fields rather than tabbing.
对于它的价值 - 在我的情况下,我的用户需要使用“ENTER KEY”,因为我们正在从终端/绿屏应用程序过渡到 Windows 应用程序,他们更习惯于通过字段“输入”而不是制表符.
All these methods worked but still kept the annoying sound until I added e.SuppressKeyPress
.
所有这些方法都有效,但在我添加e.SuppressKeyPress
.
回答by Shahrzad Saeedi
I use this code in one of the text box keydown event
我在文本框 keydown 事件之一中使用此代码
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
Unable handle this keydown
event for all text boxes in my form. Suggest something. Thanks
无法keydown
为表单中的所有文本框处理此事件。建议点什么。谢谢