2009-07-06 19 views
15

私はWinFormにいくつかのTextBoxを持っています。 Enterキーが押されたときに次のコントロールにフォーカスを移動したいですか?テキストボックスがコントロールを得るたびに、テキストも選択されるので、現在のテキストと置き換えられます。Enterキーを押して次のコントロールに移動します

これを行うにはどのような方法が最適ですか?

答えて

27

Enter as Tab:テキストボックスを継承するユーザーコントロールを作成し、KeyPressメソッドをオーバーライドします。ユーザがenterを押すと、SendKeys.Send("{TAB}")またはSystem.Windows.Forms.Control.SelectNextControl()に電話することができます。 KeyPressイベントを使用して同じことを達成できることに注意してください。

フォーカス全体テキスト:再度、オーバーライドまたはイベントを介してGotFocusイベントをターゲットにしてから、TextBox.Selectメソッドを呼び出します。

+2

は最善の解決策ではありません あなたはグリッド内の複数のTextBoxを持っていると仮定すると、それは次のようになりますタスクを達成するための方法が提供されたとき。 [SelectNextControl(http://msdn.microsoft.com/en-us/library/system.windows.forms.control.selectnextcontrol.aspx)キーを送信することなく、乱雑タブボタンの送信と同じ機能を実行することができます。 – Fr33dan

2

テキストボックスにKeyPressハンドラを配置して、どのキーが使用されているかを確認できます。

テキスト選択を処理するには、GotFocusイベントにハンドラを配置します。

また、複数行のテキストボックスを処理する方法(または必要がある場合)を検討することもできます。 KeyPressイベントで

10

、押されたユーザーが入力した場合、自動的にフォーカスを受け取るには、テキストを選択し実装するために呼び出す

SendKeys.Send("{TAB}") 

素敵な方法は、次のオーバーライドを使用して、プロジェクト内のTextBoxのサブクラスを作成することです。

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

次に、すべてのフォームでWinForms標準テキストボックスの代わりにこのカスタムテキストボックスを使用します。

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!"); 
    } 
} 
20

コード例のカップルをSelectNextControlを使用してC#で。

を押すと、次のコントロールに移動します。

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

第二は、コントロールを移動するDOWN UP矢印を使用しています。

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

+0

あなたが現在のフォームのすべてのコントロールにこれを適用したい場合は、「(コントロール)送信者」上記のコードでは、「ACTIVECONTROL」とし、TrueにフォームのプロパティKeyPreviewのを設定し、フォームのKeyUpイベントに登録して交換することができます。 –

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

ます。ただし、フォーム上の単一のメソッドを持つことは、あなたが大規模なコードベースを持つことにつながるどの使用しようとしているすべてのコントロールを再書き込み、およびそれらを後でデバッグするよりもはるかに簡単で、もちろんこれを行うことができます。 – AaA

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

}

関連する問題