none
Auto select in TextBox control

    Question

  •  

    Hi,

    I want that the text on my TextBox control will be selected each time it gets the focus (by mouse click or by TAB).

    How do I do it ?

     

    thanks,

    Yoav.

    Wednesday, April 09, 2008 12:43 PM

Answers

  • This should be able to do the trick:

    textBox.GotFocus += delegate
    {
        textBox.SelectAll();
    };

    Hope this helps
    Friday, April 11, 2008 3:11 AM
  • As far as I know, there is "no" pure XAML solution to this problem, you need to write some code.

    Thanks
    Tuesday, April 15, 2008 6:16 AM

All replies

  • This should be able to do the trick:

    textBox.GotFocus += delegate
    {
        textBox.SelectAll();
    };

    Hope this helps
    Friday, April 11, 2008 3:11 AM
  • I am just curious as to why you would want to do that ! usually selecting the entire text runs the risk if it getting deleted when you press any valid key on keyboard

    Friday, April 11, 2008 4:39 AM
  •  

    Can it be done in xaml ?
    Sunday, April 13, 2008 8:19 AM
  •  

    The reason that I want it is that I have a ListView control with editable cells (each cell is based on TextBox control).

    I want to allow the user a fast way to rename cells (when moving between cells by Tab pressing without the need to select the text each time a cell got the focus). This is similar to Excel behaviour.

    Sunday, April 13, 2008 8:24 AM
  • As far as I know, there is "no" pure XAML solution to this problem, you need to write some code.

    Thanks
    Tuesday, April 15, 2008 6:16 AM
  • I tried

    textBox.GotFocus += delegate
    {
        textBox.SelectAll();
    };


    and could not get the entire text selected each time.  Maybe I am placing this in the wrong spot.  Where should I place this code?

    Thank you in advance.
    Thursday, July 17, 2008 12:56 PM
  • Hi,

    Here is an example of where to place your code.

        public partial class Window1 : Window 
        { 
            public Window1() 
            { 
                InitializeComponent(); 
     
                textBox1.GotFocus += delegate 
                { 
                    textBox1.SelectAll(); 
                }; 
            } 
        }

    Hope this helps.


    • Edited by dave wilkes Friday, July 18, 2008 8:04 AM update code format
    • Proposed as answer by MDN-KME Sunday, June 02, 2013 6:49 AM
    Friday, July 18, 2008 7:49 AM
  • If you need to do this for a large number of textboxes (in Silverlight or WPF), then you can use the technique used in the blog post: http://dnchannel.blogspot.com/2010/01/silverlight-3-auto-select-text-in.html. It uses Attached Properties and Routed Events.
    Thursday, January 28, 2010 1:38 AM
  •  

    this for c# windows form

       private void textBox1_KeyUp(object sender, KeyEventArgs e)
                {
                if (e.KeyCode == Keys.Enter)
                    {
                    textBox2.Focus();
                    }
                }

            private void textBox2_KeyUp(object sender, KeyEventArgs e)
                {
                if (e.KeyCode == Keys.Enter)
                    {
                    textBox1.Focus();
                    }
                }

    • Proposed as answer by MDN-KME Sunday, June 02, 2013 6:49 AM
    Wednesday, May 25, 2011 4:01 PM