2009-06-11 1 views
2

내 System.Windows.Controls.RichTextBox에서 주어진 단어의 TextRange를 찾고 싶습니다. 그러나 처음 발견 된 단어 다음에 올바른 PositionAtOffset을 제공하지 않습니다. 첫 번째 단어가 맞고 다음 단어가 발견되면 단어의 위치가 올바르지 않습니다. 올바른 방법을 사용하고 있습니까? listOfWordsRichTextBox에서 TextRange를 찾는 방법 (두 TextPointer 사이)

Word= listOfWords[j].ToString(); 

startPos = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text.IndexOf(Word.Trim()); 

leftPointer = textPointer.GetPositionAtOffset(startPos + 1, LogicalDirection.Forward); 

rightPointer = textPointer.GetPositionAtOffset((startPos + 1 + Word.Length), LogicalDirection.Backward); 
TextRange myRange= new TextRange(leftPointer, rightPointer); 

답변

11

MSDN에서 샘플에서 적응이 코드를 통해

루프는 지정된 위치에서 단어를 찾을 수 있습니다.

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; 
} 

당신은 다음과 같이 사용할 수 있습니다 :

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); 
} 
+0

TextPointer.GetPositionAtOffset의 오프셋은 '기호'가 아니므로이 코드는 일반적으로 작동하지 않습니다. 대부분의 경우 문자열에 공백이 포함되거나 단어가 UIElements로 확장 될 수있는 비영어권 언어 일 수 있습니다. – Mark

0

내 생각, 그것은 또한 작동합니다.

목록에서 "다음 항목 찾기"와 같습니다.

TextPointer FindWordsFromPosition(TextPointer position, IList<string> words) 
    { 
     var firstPosition = position; 
     while (position != null) 
     { 
      if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) 
      { 
       string textRun = position.GetTextInRun(LogicalDirection.Forward); 

       var indexesInRun = new List<int>(); 

       foreach (var word in words) 
       { 
        var index = textRun.IndexOf(word); 

        if (index == 0) 
         index = textRun.IndexOf(word, 1); 

        indexesInRun.Add(index); 
       } 

       indexesInRun = indexesInRun.Where(i => i != 0 && i != -1).OrderBy(i => i).ToList(); 

       if (indexesInRun.Any()) 
       { 
        position = position.GetPositionAtOffset(indexesInRun.First()); 
        break; 
       } 

       return firstPosition; 
      } 
      else 
       position = position.GetNextContextPosition(LogicalDirection.Forward); 
     } 

     return position; 
    }