Create TextRange between characters?

111 Views Asked by At

I'm working on code that turns html tags red and text between balck, but can't get it working. I've been messing with this code but it only works with the first instance of a open and closed tag. If I go to the next line my tags don't turn red and text between doesn't turn black. I use the same type of function to turn html tags red... The function below is supposed to turn text between tags black.

Public Function FindBetweenTag(ByVal position As TextPointer,
                               ByVal startCharacter As String) As TextRange
  While position IsNot Nothing
    If position.GetPointerContext(LogicalDirection.Forward) =
                                                    TextPointerContext.Text Then
      Dim textRun As String = position.GetTextInRun(LogicalDirection.Forward)
      'find starting index of matching word
      Dim indexInRun As Long = textRun.IndexOf(startCharacter)
      Dim indexOfEndTag As Long = textRun.IndexOf("</p>")

      If indexInRun >= 0 Then
        Dim startPointer As TextPointer =
          position.GetPositionAtOffset(indexInRun + startCharacter.Length + 1)
        Dim endPointer As TextPointer =
          startPointer.GetPositionAtOffset(indexOfEndTag)
        Return New TextRange(startPointer, endPointer)
      End If
    End If

    position = position.GetNextContextPosition(LogicalDirection.Forward)
  End While
  Return Nothing
End Function

Can anyone tell me why this is only working with the first instance?

0

There are 0 best solutions below