SoFunction
Updated on 2025-03-06

Implementation method of automatically selecting all when TextBox gets input focus

C# develops WinForm, how to automatically select all when TextBox gets input focus?

Many friends will definitely find it easier to see it: Isn’t it done if you add a GotFocus event to TextBox and then call() in the event? At first, the Cangpi had to think so, but if you try to do this, you will find that when you click the left mouse button to get the input focus of TextBox, the text inside will not be selected at all.

Is this why? The reason is that when the TextBox gets the input focus through the mouse, the sequence of events triggered by the TextBox is: MouseDown->GotFocus->MouseUp, which means that the TextBox has already obtained the input focus the moment the mouse is pressed, and you can select all the text at this time. But what's depressing is that MouseUp will cancel the text selection status of TextBox... In other words, the text was actually selected, but it was immediately unselected (-_-#)

This is easy to do, so why would I change it to the MouseUp event ()? With this change, clicking TextBox with the left mouse button can really achieve the selection of all, but a new problem will be found: when you want to click TextBox again to cancel the selection of all, you will find that TextBox is still selecting all.

Based on the above description, we can roughly understand such a logic:

1. If the TextBox itself does not gain focus, click the left mouse button to obtain focus and execute the selection all.

2. If the TextBox itself has gained focus, click the left mouse button and no selection will be performed.

According to the above logic, in fact, when TextBox goes from no input focus to obtaining the input focus, it is only necessary to select all for the left mouse button click operation, otherwise the selection all operation will not be performed. Therefore, a variable can be used as the mark for TextBox from no input focus to obtaining the input focus. When the left mouse button clicks to determine that this mark exists, perform a selection all operation and cancel this mark, so that the above logic can be realized.

The following code is used as the input focus mark to achieve the above automatic selection of all logic reference content.

Copy the codeThe code is as follows:

  public Form1()        
  {             
         InitializeComponent();
         = "Auto Select Text Demo";             
         = false;             
         += new EventHandler(textBox_GotFocus);             
         += new MouseEventHandler(textBox_MouseUp);        

 }

  void textBox_MouseUp(object sender, MouseEventArgs e)        
 {            
//If the left mouse button is used and the mark exists, then select all will be performed
       if ( == && (bool) == true)           
         {                 
                 ();            
         }

//Cancel all sign
      = false;        
 }

 
 void textBox_GotFocus(object sender, EventArgs e)       
  {             
= true;    //Set the marker
();    //Note 1
 }

It is worth mentioning that although the MouseUp event has already executed the Select All, we still have to perform Select All in the GotFocus event in the "Note 1" position in the code. The reason is that the method of allowing TextBox to gain focus is not only through mouse clicks, but also through Tab to switch focus. MouseUp will not be triggered at this time, but there will be no problem of canceling the Select All in MouseUp. Therefore, it is still necessary to perform Select All in the GotFocus event.