I am trying to set TextBox caret position according to the mouse coordinates when hovering over the TextBox. I can obtain the mouse coordinates relative to the TextBox by using Mouse.GetPosition(this.MyTextBox) but I need help translating those coordinates into the caret position. Any help would be greatly appreciated!
Asked
Active
Viewed 4,190 times
2
-
I think you should try simulating mouse click event in you TextBox and pass the mouse position as event arguments – May 15 '12 at 23:06
-
See [this answer](http://stackoverflow.com/a/4619597/620360) in related question. – LPL May 16 '12 at 07:21
2 Answers
4
MyTextBox.CaptureMouse();
MyTextBox.CaretIndex = MyTextBox.GetCharacterIndexFromPoint(Mouse.GetPosition(MyTextBox), true);

The Code Guy
- 311
- 5
- 10
-1
MyTextBox.GetCharacterIndexFromPoint(..) this returns the zero based index of the character that is closest to the specified point. please see the link below: http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.getcharacterindexfrompoint.aspx

Soma Sarkar
- 836
- 6
- 7