0

Given a TextPointer, how do I get a TextPointer to the same horizontal position in the previous / next line?

Basically the same as if the user pressed the up/down key in a RichTextBox.

devar
  • 95
  • 8
  • what if the next line is shorter than the current line so that the current position becomes out of range in the next line? – King King Oct 12 '14 at 18:04
  • Then I need a pointer to the end of that line - the same as when using the up/down arrow keys in a RichTextBox. – devar Oct 12 '14 at 18:17
  • Have you tried anything? [`TextPointer`](http://msdn.microsoft.com/en-us/library/System.Windows.Documents.TextPointer%28v=vs.110%29.aspx) has 3 methods that could be used to do this: `GetLineStartPosition`, `GetOffsetToPosition`, and `GetPositionAtOffset`. Try something and then come back with what you did and what issues you are having. – clcto Oct 12 '14 at 18:21
  • I haven't tried GetOffsetToPosition/GetPositionAtOffset, because it'd probably only work with monospaced fonts. – devar Oct 12 '14 at 18:32
  • You can try using Automation with `ITextRange` pattern. However the way arrow keys work is not like what you thought. I've just tested it. In fact it jumps to the closest character (right above for up arrow key and right below for down arrow key), so the count of characters (from the beginning of the line to the current position) may be different (between positions). – King King Oct 12 '14 at 19:55
  • @KingKing: Exactly :-) That's why I haven't taken GetOffsetToPosition/GetPositionAtOffset into account. – devar Oct 12 '14 at 21:23
  • I wonder how RichTextBox handles up- and down-keys. To find the closest character, you need some layout information, i.e. where the individual characters are displayed. Probably, the FlowDocument does not have this information, but only the control displaying it. – devar Oct 12 '14 at 21:30
  • 1
    I believe that if you can solve this, you have to use Automation. Consider the method `ITextProvider.RangeFromPoint`. First find the current Point (in screen coordinates), then use `RangeFromPoint` to get an `ITextRangeProvider`. That interface has some interesting methods to play with, such as the `ExpandToEnclosingUnit` (with `TextUnit.Character`, try calling `Select()` method. Then you can access the `Selection` property of the RichTextBox and pick up either the `Start` or `End`. I'm not sure if this works. But it's the direction you should try. I don't have time to try it myself now. – King King Oct 12 '14 at 21:39

0 Answers0