TextBox gets the implementation of automatically selecting all of the input focus

  • 2020-05-10 18:39:23
  • OfStack

          C# development WinForm, how can TextBox automatically select all input focus?

          no doubt many friends will find it easy: add GotFocus event to TextBox and then call TextBox.SelectAll () in the event. Nestpi 1 was supposed to do that at first, but if you try, you'll find that when you click the left mouse button to get TextBox. To get the input focus, you won't be able to select all the text.

          this is why? The reason is that when TextBox is given input focus by the mouse, the sequence of events triggered by TextBox is: MouseDown- > GotFocus- > MouseUp, which means that TextBox has gained input focus at the moment the mouse is pressed, and can select all the text. Sadly, MouseUp cancels the text selection of TextBox... This means that the text was actually selected, but it was immediately unselected (-_-#)

       ,        , SelectAll() With this change, clicking TextBox with the left mouse button can actually select all of them. However, a new problem will be found: when you want to left-click TextBox again to cancel all of them, you will find that TextBox is still selecting all of them.

According to the above description, the following logic can be roughly understood:

        1. If TextBox itself does not get focus, click the left mouse button to get focus, then select all.

        2. If TextBox itself has gained focus, clicking the left mouse button will no longer select all.

      according to the above logic, in fact as long as the TextBox from no input focus to get the input focus, mouse the left key click operation against doing one selection, otherwise don't do selection, so you can use a variable as TextBox from no input focus to the tag, get the input focus when the left mouse button click to recognize this tag to exist, perform one selection, and cancel this tag, so that you can achieve the above logic.

The code below         USES TextBox.Tag as the input focus marker to achieve the above automatic full selection of the logical reference content            


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

 }

  void textBox_MouseUp(object sender, MouseEventArgs e)         
 {             
         // If the left mouse button operates and the flag exists, select all              
       if (e.Button == MouseButtons.Left && (bool)textBox.Tag == true)            
         {                  
                 textBox.SelectAll();             
         }

        // Unselect all flags               
      textBox.Tag = false;         
 }

 
 void textBox_GotFocus(object sender, EventArgs e)        
  {              
            textBox.Tag = true;    // Set the tag               
         textBox.SelectAll();   // Pay attention to 1         
 }

Is worth questioning, although MouseUp event has carried out the selection, but the code in the "note 1" position, we still need to perform 1 GotFocus event time selection, the reason is let TextBox gains focus method, in addition to the mouse to click, may also by Tab switching focus, at this time will not trigger MouseUp, but also so there won't be canceled by MouseUp selection problem, therefore in GotFocus event or it is necessary to execute one selection.


Related articles: