Reputation: 61
I have 3 Textboxes in my app page and i want to set tab index for them so that when user presses return key from keyboard it should go to next text box. I have set the Tab Index property of TextBox but its not working.
Upvotes: 0
Views: 114
Reputation: 61
It works for windows phone 8.1 app with the following code.
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key==Windows.System.VirtualKey.Enter)
{
FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
}
}
use the above method in KeyDown event of all TextBoxes.
Upvotes: 1
Reputation: 33
This is a possible implementation.
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
>
<TextBox TabIndex="0" KeyDown="OnKeyDown"/>
<TextBox TabIndex="1" KeyDown="OnKeyDown"/>
<TextBox TabIndex="2" KeyDown="OnKeyDown"/>
<TextBox TabIndex="3" KeyDown="OnKeyDown"/>
<TextBox TabIndex="4" KeyDown="OnKeyDown"/>
</StackPanel>
This next code assume that the ContentPanel contains only TextBox. It's up to you to add more smart code in it...
private void OnKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
var txtBox = sender as TextBox;
var index = txtBox.TabIndex;
var nextTextBox = ContentPanel.Children.Cast<TextBox>().FirstOrDefault(t => t.TabIndex == index + 1);
if (nextTextBox != null)
{
nextTextBox.Focus();
}
}
}
Upvotes: 0