How to select all the text in a text box when it receives focus

On a Windows phone, how can I select all the text in the text box when the TextBox has focus?

I am trying to set the get focus property in a text box:

private void TextBox_GotFocus(object sender, RoutedEventArgs e) { TextBox textBox = (TextBox)sender; textBox .SelectAll(); } 

What I see, I see that all text is selected within 1-2 seconds, and then it returns to cursor mode (i.e. 1 blinking line).

+6
source share
2 answers

I had the same problem in WPF and I managed to solve it. Not sure if you can use what I used, but essentially your code will look like this:

  private void TextBox_GotFocus(object sender, RoutedEventArgs e) { TextBox textBox = (TextBox)sender; textBox .CaptureMouse() } private void TextBox_GotMouseCapture(object sender, RoutedEventArgs e) { TextBox textBox = (TextBox)sender; textBox.SelectAll(); } private void TextBox_IsMouseCaptureWithinChanged(object sender, RoutedEventArgs e) { TextBox textBox = (TextBox)sender; textBox.SelectAll(); } 

All events are connected to the source text box. If this does not work for you, perhaps you can replace CaptureMouse with CaptureTouch (and use the appropriate events). Good luck

0
source

You can try this code,

  private void TextBox_GotFocus(object sender, RoutedEventArgs e) { String sSelectedText = mytextbox.SelectedText; } 

If the user clicks on the copy icon that appears after the selection, he will be copied, if you want to do it programmatically, you can try this

 DataPackage d = new DataPackage(); d.SetText(selectedText); Clipboard.SetContent(d); 

I would suggest making a copy in some other event, rather than gotfocus, since this will be launched immediately after the user presses the text field, so that this method is called when the text has not been entered.

0
source

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


All Articles