0

I would like to know what exactly happen when we use myTextPointer.GetPositionAtOffset(int).

I am doing a search in a richTextBox using this code. I just don't understand why we need to be in a loop. I check in debug mode and most of the time when we do searchRange.Start.GetPositionAtOffset(offset + searchText.Length) my textPointer didn't move, and my "searchText" had a length of 1 or more.

In the loop, result is mostly empty cause new TextRange(start, start.GetPositionAtOffset(searchText.Length) has a range of offset 87 and 87 (example) even if searchText.Length is higher then 0.

for(TextPointer start = searchRange.Start.GetPositionAtOffset(offset); start != searchRange.End; start = start.GetPositionAtOffset(1))
{
    TextRange result = new TextRange(start, start.GetPositionAtOffset(searchText.Length);
    if(result.Text == searchText)
        return result;
}

Thanks

Community
  • 1
  • 1
Northik
  • 342
  • 2
  • 11
  • Did you read explanation of accepted answer in linked question? *The reason for the for() loop in FindTextInRangeUnfortunately the range.Text strips out non-text characters, so in some cases the offset computed by IndexOf will be slightly too low* – Sinatr Oct 30 '14 at 14:23
  • Yeah but it's talking about the indexOf and not about the getPositionAtOffset. – Northik Oct 30 '14 at 14:38
  • My question is more like, why when we do `getPositionAtOffset()` the textpointer don't move? I don't think the indexOf is for `getPositionAtOffset()`, because I don't see why there would have an indexOf in this method. – Northik Oct 30 '14 at 14:42
  • `GetPositionAtOffset` simply helps you derive another `TextPointer` based on some offset with the current TextPointer, that's all. There is not any additional visual effect (related or not related to the caret) here. – King King Oct 30 '14 at 18:00

0 Answers0