Is GetPositionAtOffset equivalent for text only?

Is there a nice solution to the equivalent of GetPositionAtOffset() that only considers text input positions instead of all characters?

An example of motivation in C #:

 TextRange GetRange(RichTextBox rtb, int startIndex, int length) { TextPointer startPointer = rtb.Document.ContentStart.GetPositionAtOffset(startIndex); TextPointer endPointer = startPointer.GetPositionAtOffset(length); return new TextRange(startPointer, endPointer); } 

Edit: So far, I have "decided" it like this

 public static TextPointer GetInsertionPositionAtOffset(this TextPointer position, int offset, LogicalDirection direction) { if (!position.IsAtInsertionPosition) position = position.GetNextInsertionPosition(direction); while (offset > 0 && position != null) { position = position.GetNextInsertionPosition(direction); offset--; if (Environment.NewLine.Length == 2 && position != null && position.IsAtLineStartPosition) offset --; } return position; } 
+6
source share
2 answers

As far as I know, no. My suggestion is that for this purpose you create your own GetPositionAtOffset method. You can check which PointerContext is in the TextPointer using:

 TextPointer.GetPointerContext(LogicalDirection); 

To get the following TextPointer that points to another PointerContext:

 TextPointer.GetNextContextPosition(LogicalDirection); 

The sample code I used in a recent project ensures that the context of the pointer is of type Text by looping until it is found. You can use this in your implementation and skip the offset increment if it is found:

 // for a TextPointer start while (start.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text) { start = start.GetNextContextPosition(LogicalDirection.Forward); if (start == null) return; } 

I hope you can use this information.

+2
source

Could not find an effective solution to this problem for a long time. The following code fragment works in my case with the highest performance. Hope this helps someone too.

 TextPointer startPos = rtb.Document.ContentStart.GetPositionAtOffset(searchWordIndex, LogicalDirection.Forward); startPos = startPos.CorrectPosition(searchWord, FindDialog.IsCaseSensitive); if (startPos != null) { TextPointer endPos = startPos.GetPositionAtOffset(textLength, LogicalDirection.Forward); if (endPos != null) { rtb.Selection.Select(startPos, endPos); } } public static TextPointer CorrectPosition(this TextPointer position, string word, bool caseSensitive) { TextPointer start = null; while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); int indexInRun = textRun.IndexOf(word, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase); if (indexInRun >= 0) { start = position.GetPositionAtOffset(indexInRun); break; } } position = position.GetNextContextPosition(LogicalDirection.Forward); } return start; } 
0
source

All Articles