Im looking for a quick solution for this problem I have. In my app I have a richtextbox, I want to make a code that will capture the current position of the cursor and display it on the richtextbox as a mouse coordinate. the cursor's position is screen wise and not just focused on the app itself. All help will be widely appreciated.
Asked
Active
Viewed 1,692 times
2 Answers
0
TextBoxBase.SelectionStart and TextBoxBase.SelectionLength can be used . See the examples also.
You may want to check C# Windows Forms RichTextBox cursor position also .

Community
- 1
- 1

Sandeep Pathak
- 10,567
- 8
- 45
- 57
0
I'm not sure if you really mean "cursor" or if you mean mouse pointer. Both terms are used to refer to the mouse pointer, but of course "cursor" used to just mean the little flashy thing that tells you where you're typing.
If you want the screen coordinates of the mouse pointer, use the static Position
property of the Cursor
(see what I mean?) class:
richTextBox.Text = Cursor.Position.ToString();

Igby Largeman
- 16,495
- 3
- 60
- 86
-
thanks that worked very well, one question though, how would I be able to fire the code when I press a keyboard key? – mendez Aug 04 '11 at 06:48
-
There are a few different options for that. Have a look at the answers for [this question](http://stackoverflow.com/questions/5951496/how-do-i-capture-keys-f1-regardless-of-the-focused-control-on-a-form). And if my answer satisfied your original question, please mark it as accepted. – Igby Largeman Aug 04 '11 at 14:52
-
Everything works fine but for one last problem, I have a button that clears text for the richtextbox but for some reason it doesn't clear the coordinates. how can I resolve this? I have the following code placed so I can have multiple coordinates in the RTB richTextBox1.AppendText(Cursor.Position.ToString()); – mendez Aug 04 '11 at 20:03
-
Doesn't clear the coordinates, meaning the text stays in the RTB? Are you sure the coordinates aren't just getting written back to the RTB immediately after you clear it? – Igby Largeman Aug 04 '11 at 20:45
-
I don't think so, it only writes when I have the insertion point in the RTB and press any key. – mendez Aug 05 '11 at 00:36