This code, adapted from a sample on MSDN , will find words from the specified position.
TextRange FindWordFromPosition(TextPointer position, string word)
{
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
string textRun = position.GetTextInRun(LogicalDirection.Forward);
int indexInRun = textRun.IndexOf(word);
if (indexInRun >= 0)
{
TextPointer start = position.GetPositionAtOffset(indexInRun);
TextPointer end = start.GetPositionAtOffset(word.Length);
return new TextRange(start, end);
}
}
position = position.GetNextContextPosition(LogicalDirection.Forward);
}
return null;
}
Then you can use it like this:
string[] listOfWords = new string[] { "Word", "Text", "Etc", };
for (int j = 0; j < listOfWords.Length; j++)
{
string Word = listOfWords[j].ToString();
TextRange myRange = FindWordFromPosition(x_RichBox.Document.ContentStart, Word);
}
source
share