0

I have a System.Windows.Controls.TextBox and if the user presses the enter-key, i want the application to handle it like a tab-key, so that the user can jump to the next input control, like he would normally do with the tab key. Is this somehow possible?

P.S.: I couldn't find a answer without usings of "System.Windows.Forms.SendKeys.SendWait("{Tab}");", which i would not like to include in my WPF-Application.

SomeDev
  • 27
  • 5

3 Answers3

2

I don't know if is what you want, but in the code you can use the KeyDown event to have something like:

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    try
    {
        if (e.KeyCode == Keys.TheKeyYouWant)
        {
          NextElementToBeFocus.Focus();
        }
    }
    catch(Exception error)
    {
        //code for any other type of exception
    }

}

With that you can have the key you want to act like a Tab.

  • thanks for your reply, but i was looking for a more common approach as i already used this kind of pattern in another project of mine. – SomeDev Jul 06 '21 at 14:31
0

Inside of the textbox keydown event enter this code:

If (e.KeyCode == Keys.Enter)
    {
        This.SelectNextControl(TextBox1, True, False, False, False);
    }

The name of the Textbox is TextBox1.

0

Set TextBox1.AcceptsReturn = false;
Add a KeyDown/KeyUp/KeyPress handler to the textbox and if the key is Enter or Return, invoke
TextBox1.MoveFocus (new TraversalRequest(FocusNavigationDirection.Next));

lidqy
  • 1,891
  • 1
  • 9
  • 11