Start New Discussion within our Software Development Community

What I would like to do is get an integer index of a TextPointer, as if the flowDocument was just a normal string. So basically I want to replicate the indexOf method of String, but for a flowDocument instead.
I currently have this partially working solution:

private int GetIntFromPointer(TextPointer mark, FlowDocument document)
        {
            int count=0;
            TextPointer startPointer = document.ContentStart;
            while(startPointer.CompareTo(mark) < 0)
            {
                if(startPointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                    count++;
                startPointer = startPointer.GetNextInsertionPosition(LogicalDirection.Forward);
                if(startPointer.CompareTo(document.ContentEnd)==0)
                    return count;
            }
            return count;
        }

Unfortunately, the resulting index returned by GetIntFromPointer seems to be inaccurate as it attempts to determine the index of TextPointers which are far in the flowDocument. And I don't know why that is the case. I am checking the pointerContext to be sure I am only incrementing when I am looking at text, but I guess there is something I don't understand. Are escape character's being counted?
Otherwise, this method is used in conjunction with a method which accepts an integer index and returns a TextPointer and I am quite confident that method is reliable.

Any ideas as to what I am doing wrong in this method?

This article has been dead for over six months. Start a new discussion instead.