c#-winform-how to get a text box (textbox) in the mouse, cursor position

Source: Internet
Author: User

The mouse position and cursor position in the text box (textbox) is two different concepts, the mouse position is the key to click the Mouse (Nousedown) to obtain, and the cursor position is real-time to obtain, that is, the user input a character (KeyUp), this position will change once, No action is required for the mouse.

The following code can get both the mouse and the cursor position, where the "position" refers to the number of the string in the first.

You need to add a text box (TextBox1) to the form and bind the Textbox1_keyup and Textbox1_mousedown events.

To achieve the cursor position after clicking or pressing the mouse, use the following code when available

First, press the key after the position-as long as by the key cursor position changes (mouse click the cursor changes, Label2.Text does not change):

Private void Textbox1_keyup (object  sender, KeyEventArgs e)         {             label2. Text = textBox1.SelectionStart.ToString ();         }  

Second, press the mouse position-as long as by pressing the mouse cursor position changes (through the key cursor changes, Label2.Text does not change):

Private void Textbox1_mousedown (object  sender, MouseEventArgs e)         {             =  TextBox1.SelectionStart.ToString ();         }  

c#-winform-how to get a text box (textbox) in the mouse, cursor position

Related Article

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.