This code adapted from a sample at MSDN will find words in from a specified position.
TextRange FindWordFromPosition(TextPointer position, string word)
{
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
string textRun = position.GetTextInRun(LogicalDirection.Forward);
// Find the starting index of any substring that matches "word".
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);
}
// position will be null if "word" is not found.
return null;
}
You can then use it like so:
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);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…