The implementation method of automatic selection when TextBox gets input focus _c# tutorial

Source: Internet
Author: User

C # Development WinForm, how to implement the textbox to get input focus automatically select all?

I'm sure a lot of friends will find it easier to read: Add a GotFocus event to the textbox, and then call Textbox.selectall in the event () is it over? The nest skin was taken for granted at first, but if you try to do it, you'll find it. When you click the left mouse button to make the TextBox. When you get the input focus, the text in it is not all selected.

This is why? The reason is that when the textbox gets input focus through the mouse, the sequence of events triggered by the TextBox is: Mousedown->gotfocus->mouseup, which means that the textbox has got the input focus at the moment the mouse is pressed, You can select all of the text at this time. But the depressing is that MouseUp will cancel the textbox's text selection ... That is, the text was actually selected, but was immediately unchecked (-_-#)

This is good to do ah, then I change in the MouseUp incident Textbox.selectall () not become? This change, the left mouse button click on the textbox can really achieve the full election, but the new problem will be found: when you want to left again click the textbox to deselect all, you will find that the textbox is still in full selection.

Based on the above description, it is possible to get a sense of this logic:

1, if the textbox itself does not have the focus, then click the left mouse button to get focus after the implementation of the full selection.

2. If the textbox itself has the focus, click the left mouse button to no longer perform the full selection.
  

According to the above logic, in fact, as long as the textbox from no input focus to get input focus, for the left mouse button click operation to do a full selection, otherwise do not select all operation, so you can use a variable as a textbox from no input focus to get input focus of the tag, When you click the left mouse button to determine the existence of this tag, perform a full selection operation, and the tag is canceled, so that the logic can be implemented.

The following code uses TEXTBOX.TAG as the input focus tag to achieve the above automatic full selection logical reference content

Copy Code code 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 operation and the tag is present, perform the Select all
if (E.button = = MouseButtons.Left && (bool) Textbox.tag = = True)
{
Textbox.selectall ();
}

Deselect all marks
Textbox.tag = false;
}


void Textbox_gotfocus (object sender, EventArgs e)
{
Textbox.tag = true; Set markup
Textbox.selectall (); Note 1
}

It is worth noting that although the MouseUp event has been executed with the "Note 1" position in the code, we still have to perform a full selection in the GotFocus event, because the method of getting the textbox to focus, in addition to clicking through the mouse, can also be achieved through the tab switching focus. MouseUp is not triggered at this time, but there is no question of MouseUp canceling the selection, so it is necessary to perform 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.