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