Search code examples
c#windows-phone-8.1

How to select all text in textbox when it gets focus


In Windows phone, how can I select all text in Textbox when the TextBox has focus?

I try setting the get focus property of Textbox:

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

        textBox .SelectAll();
    }

What I see is I see all the text is being selected for 1-2 sec and then it goes back to cursor mode (i.e. 1 blink line).


Solution

  • I had this same problem on WPF and managed to solve it. Not sure if you can use what I used but essentially your code would look like:

        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 hooked up to the original textbox. If this doesn't work for you, maybe you can replace CaptureMouse with CaptureTouch (and use the appropriate events). Good luck!