按Enter键移动到下一个控件

问题描述:

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

这样做的最好方法是什么?

Tab as Enter:创建一个继承文本框的用户控件,覆盖KeyPress方法。如果用户按下输入键,则可以拨打SendKeys.Send("{TAB}")System.Windows.Forms.Control.SelectNextControl()。请注意,您可以使用KeyPress事件实现相同。

重点整个文本:再次,通过覆盖或事件,目标GotFocus事件,然后调用TextBox.Select方法。

+2

发送键击是不是最好的解决办法当提供一种方法来完成任务时。 [SelectNextControl](http://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.selectnextcontrol.aspx)可以执行与发送选项卡按钮相同的功能,而不会发送密钥混乱。 – Fr33dan 2013-08-28 13:57:35

您可以在您的文本框中放入KeyPress处理程序,并查看使用了哪个键。

要处理文本选择,请在GotFocus事件中添加处理程序。

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

在按键事件,如果按用户输入,呼叫

SendKeys.Send("{TAB}") 

自动执行对接收焦点选择文本最好的方法是在你的项目中创建文本框的子类具有以下重写:

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

然后使用此自定义文本框代替所有窗体上的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!"); 
    } 
} 
+0

这只是答案的一部分... – 2014-10-23 17:07:46

一对夫妇的代码示例使用SelectNextControl C#。

第一次移动到下一个控件时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); 
     } 
    } 

第二步使用UPDOWN箭头通过控制移动。

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 Method

+0

我最喜欢的答案。这假定开发人员已经正确设置了他们应该始终执行的控件的TabStop和TabIndex属性。 – user3902302 2016-04-28 18:43:50

+0

如果要将此应用于当前窗体的所有控件,可以将窗体属性KeyPreview设置为True,注册到窗体的KeyUp事件,并将“(Control)sender”替换为上面代码中的“ActiveControl”。 – 2016-10-27 10:16:52

你也可以写你自己的这种控制,如果你想更频繁地使用这个。 假设你有多个文本框在一个网格,它会是这个样子:

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; 

    } 


    /// <summary> 
    /// Text for the TextBox 
    /// </summary> 
    public String Text 
    { 
     get { return _TextBox.Text; } 
     set { _TextBox.Text = value; } 
    } 


    /// <summary> 
    /// Inputscope for the Custom Textbox 
    /// </summary> 
    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}") 

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(); 
    } 

}