How to automatically select all input focus values when TextBox is used

Source: Internet
Author: User

C # How to automatically select all input focus values for TextBox when developing WinForm?

Certainly many of my friends will find it easy to see: Add a GotFocus event to the TextBox, and then call TextBox. SelectAll () in the event. Isn't that all done? At the beginning, nest skin naturally thinks so, but if you try to do so, you will find that when you click the left mouse button to make TextBox. when the input focus is obtained, the text in it is not all selected.

This is why? The reason is that when the TextBox gets the input focus through the mouse, the event sequence triggered by the TextBox is: MouseDown-> GotFocus-> MouseUp, that is, the TextBox has obtained the input focus at the moment when the mouse is pressed, you can select all texts. However, it is depressing that MouseUp will cancel the selection of TextBox text... that is to say, the text has been fully selected, but is immediately deselected (-_-#)

If this is the case, why can't TextBox. SelectAll () be changed in the MouseUp event? With this change, you can select all items by clicking the TextBox with the left mouse button. However, if you want to click the TextBox again to cancel the selection, you will find that the TextBox is still fully selected.

According to the above description, we can find out the logic as follows:

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

2. If the TextBox itself has obtained the focus, click the left mouse button and select all.
  

According to the above logic, you only need to select the entire option from the left-click operation of the TextBox from no input focus to the obtained input focus. Otherwise, you will not select all options, therefore, you can use a variable as the flag for the TextBox from no input focus to getting the input focus. When you click the left mouse button to determine that the flag exists, perform the Select All operation and cancel the flag, in this way, the above logic can be implemented.

The following code uses TextBox. Tag to obtain the input focus Tag.

Copy codeThe Code is as follows: 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 is marked and exists, select all
If (e. Button = MouseButtons. Left & (bool) textBox. Tag = true)
{
TextBox. SelectAll ();
}

// Cancel all Selection
TextBox. Tag = false;
}

Void textBox_GotFocus (object sender, EventArgs e)
{
TextBox. Tag = true; // set the Tag
TextBox. SelectAll (); // Note 1
}

It is worth noting that although the MouseUp event has been executed to select all, but the "NOTE 1" position in the code, we still need to execute the "select all" option in the GotFocus event again, the reason is that the TextBox can get the focus. Besides clicking the mouse, you may also switch the focus through the Tab. In this case, MouseUp is not triggered, therefore, it is not necessary to cancel full selection by MouseUp. Therefore, it is necessary to execute a full selection in the GotFocus event.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.