按Enter键移至下一个控件

我在WinForm上有几个TextBox。 当按下Enter键时,我希望焦点移动到下一个控件? 每当文本框获得控制权时,它也会选择文本,以便任何编辑都将替换当前文本。

做这个的最好方式是什么?

C#中使用SelectNextControl的几个代码示例。

当按下ENTER时,第一个移动到下一个控件。

  private void Control_KeyUp( object sender, KeyEventArgs e ) { if( (e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return) ) { this.SelectNextControl( (Control)sender, true, true, true, true ); } } 

第二个使用向上向下箭头在控件中移动。

  private void Control_KeyUp( object sender, KeyEventArgs e ) { if( e.KeyCode == Keys.Up ) { this.SelectNextControl( (Control)sender, false, true, true, true ); } else if( e.KeyCode == Keys.Down ) { this.SelectNextControl( (Control)sender, true, true, true, true ); } } 

请参阅MSDN SelectNextControl方法

在KeyPress事件中,如果用户按Enter键,则调用

 SendKeys.Send("{TAB}") 

实现在接收焦点上自动选择文本的最好方法是在项目中使用以下覆盖创建TextBox的子类:

 Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs) SelectionStart = 0 SelectionLength = Text.Length MyBase.OnGotFocus(e) End Sub 

然后使用此自定义TextBox代替所有表单上的WinForms标准TextBox。

这可能有所帮助:

 private void textBox1_KeyDown(object sender, KeyEventArgs e) { // // Detect the KeyEventArg's key enumerated constant. // if (e.KeyCode == Keys.Enter) { MessageBox.Show("You pressed enter! Good job!"); } } 

您可以在KeyPress上放置一个KeyPress处理程序,并查看使用了哪个键。

要处理文本选择,请在GotFocus事件上放置处理程序。

您可能还想考虑如何(或者如果您需要)处理多行TextBox。

 private void txt_invoice_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) txt_date.Focus(); } private void txt_date_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) txt_patientname.Focus(); } 

}

您也可以为此编写自己的控件,以防您想更频繁地使用它。 假设你在Grid中有多个TextBox,它看起来像这样:

 public class AdvanceOnEnterTextBox : UserControl { TextBox _TextBox; public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(AdvanceOnEnterTextBox), null); public static readonly DependencyProperty InputScopeProperty = DependencyProperty.Register("InputScope", typeof(InputScope), typeof(AdvanceOnEnterTextBox), null); public AdvanceOnEnterTextBox() { _TextBox = new TextBox(); _TextBox.KeyDown += customKeyDown; Content = _TextBox; } ///  /// Text for the TextBox ///  public String Text { get { return _TextBox.Text; } set { _TextBox.Text = value; } } ///  /// Inputscope for the Custom Textbox ///  public InputScope InputScope { get { return _TextBox.InputScope; } set { _TextBox.InputScope = value; } } void customKeyDown(object sender, KeyEventArgs e) { if (!e.Key.Equals(Key.Enter)) return; var element = ((TextBox)sender).Parent as AdvanceOnEnterTextBox; if (element != null) { int currentElementPosition = ((Grid)element.Parent).Children.IndexOf(element); try { // Jump to the next AdvanceOnEnterTextBox (assuming, that Labels are inbetween). ((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition + 2)).Focus(); } catch (Exception) { // Close Keypad if this was the last AdvanceOnEnterTextBox ((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition)).IsEnabled = false; ((AdvanceOnEnterTextBox)((Grid)element.Parent).Children.ElementAt(currentElementPosition)).IsEnabled = true; } } } } 

尝试使用:

 SendKeys.Send("{TAB}")