Matthieu Oger
Matthieu Oger

Reputation: 833

Move the focus from a TextBox to another one when pressing the enter key of the soft keyboard

I'm on Windows Phone. I want to replicate the behavior of the calendar app when you create a new event. If you touch the name textbox, and press the enter key of the soft keyboard, the app moves the focus from this textbox to the one just below.

I don't know how to do that in my own app.

I guessed that I had to use the IsTabStop + TabIndex properties, and perhaps the TabNavigation.

But if I set them, it changes nothing.

Like that :

<TextBox
  IsTabStop="True"
  TabIndex="1" />
<TextBox
  IsTabStop="True"
  TabIndex="2" />

It's a simple behavior, I cannot understand why I'm not able to figure out that alone.

Thanks.

Upvotes: 3

Views: 6041

Answers (2)

Valryon
Valryon

Reputation: 368

I see two potential ways to do this.

In the first case, you know the fields that comes before and after, so you can hard code the behavior. When the user press "Enter" on textbox1, you focus on textbox2, and so on.

EDIT : This is exactly what said @milan-aggarwal

The second and more generic idea would be to use the VisualTreeHelper (http://msdn.microsoft.com/fr-fr/library/system.windows.media.visualtreehelper.aspx).

When the user press "Enter" (this is easy to detect), using the VisualTreeHelper you look at the children of the englobing panel for the next Textbox (or another type of UI component) to select and focus.

I don't know if there is a native solution to do the same thing.

Upvotes: 4

Milan Aggarwal
Milan Aggarwal

Reputation: 5104

Use KeyDown event for textBox1. And check for if (e.Key == Key.Enter || e.PlatformKeyCode == 0x0A) and change your focus to another textbox by textBox2.Focus()

Upvotes: 5

Related Questions