Moving cursor to next textbox automatically in UWP

633 views Asked by At

I have 8 small textboxes with a maxlength of 1, and I want to make it so when somebody types a character in one of the boxes it automatically moves the cursor to the next box without the user having to press tab or manually click the next box in win 10 UWp Is there an easy way to do this?

1

There are 1 answers

0
Xie Steven On

it automatically moves the cursor to the next box without the user having to press tab or manually click the next box in win 10 UWp Is there an easy way to do this?

The Focus method would be that you want. Pass FocusState.Keyboard as the parameter if you’re setting focus as a result of a keyboard interaction, like a tab sequence or key press.

I have made a simple code sample for your reference:

<StackPanel Orientation="Horizontal" Height="50">
        <TextBox x:Name="txb1" MaxLength="1" Width="20" TextChanged="txb_TextChanged"></TextBox>
        <TextBox x:Name="txb2" MaxLength="1" Width="20" TextChanged="txb_TextChanged" Margin="10 0 0 0"></TextBox>
</StackPanel>

private void txb_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textbox = sender as TextBox;
    if (!string.IsNullOrEmpty(textbox.Text.Trim()))
    {
        txb2.Focus(FocusState.Keyboard);
    }
}

enter image description here