2009-07-06 77 views
15

我在WinForm上有幾個TextBox。當按Enter鍵時,我希望焦點轉移到下一個控件?每當文本框獲得控制權時,它也將選擇文本,以便任何編輯將替換當前文本。按Enter鍵移動到下一個控件

這樣做的最好方法是什麼?

回答

27

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

2

您可以在您的文本框中放入KeyPress處理程序,並查看使用了哪個鍵。

要處理文本選擇,請在GotFocus事件中添加處理程序。

您可能還想考慮如何(或者如果您需要)處理多行文本框。

10

在按鍵事件,如果按用戶輸入,呼叫

SendKeys.Send("{TAB}") 

自動執行對接收焦點選擇文本最好的方法是在你的項目中創建文本框的子類具有以下重寫:

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

然後使用此自定義文本框代替所有窗體上的WinForms標準TextBox。

2

這可能會幫助:

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

20

一對夫婦的代碼示例使用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

0

你也可以寫你自己的這種控制,如果你想更頻繁地使用這個。 假設你有多個文本框在一個網格,它會是這個樣子:

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; 
      } 
     } 
    } 
} 
0

嘗試使用:

SendKeys.Send("{TAB}") 
1
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(); 
    } 

}

相關問題