Silverlight TextBox moving carriage position

I am trying to make the TextBox behavior for the on-screen keyboard, it can update the text in the text field, but I can not get it to focus the text field and move the cursor to the end of the text after the update is completed.

I tried using TextBox.Focus () and TextBox.Select () in both orders with no luck.

thank you for your time

+3
source share
3 answers

Changing the focus from the input event handler (i.e., clicking on a virtual key) will fail because the mouse event (or key) will return focus to the original element. To make it work, you need to send the focus command later using the object Dispatcher.

Example:

Dispatcher.Invoke(new Action(() =>
{
    textBox1.Focus();
    textBox1.SelectionStart = textBox1.Text.Length;
    // or textBox1.Select(textBox1.Text.Length,0);
}), System.Windows.Threading.DispatcherPriority.Background);
+6
source

Here is a simple example of moving the cursor to the end of a text field after updating it.

TextBox.Focus();
TextBox.Text = "sometext ";
TextBox.SelectionStart = TextBox.Text.Length;
+2
source

, "TextChanged" , .

<TextBox Name="tbTextEntry" Width="200" HorizontalAlignment="Center" Background="Plum" Text="{Binding Entered_text,Mode=TwoWay}" TextChanged="OnTextChanged_Handler"></TextBox>

, ( ):

protected void OnTextChanged_Handler(object sender, RoutedEventArgs e)
{
    TextBox my_text_box = (TextBox)sender;
    my_text_box.SelectionStart = my_text_box.Text.Length;
    Debug.WriteLine("OnTextChanged_Handler called !");
    return;
}

, , , . !

, . .

+1

Source: https://habr.com/ru/post/1727206/


All Articles