2012-07-31 60 views
4



我想知道我怎麼能得到這個詞是當前光標是在WPF的RichTextBox。我知道RichTextBox具有選擇屬性。但是,這隻會給我在RichTextBox中突出顯示的文本。相反,我想知道即使整個單詞沒有突出顯示,光標所在的單詞也是如此。

任何提示將不勝感激。

非常感謝。方式來獲得這個詞光標上,在WPF RichTextBox控件

回答

1

您可以通過CaretPosition獲取光標的當前位置。

不幸的是,沒有簡單的方法將字符拖到插入位置的左側/右側。我知道從RichTextBox中獲取文本的唯一方法是this answer,這有點複雜。但它會完成必要的。

+0

非常感謝您的回覆。我非常感謝您指向CaretPosition。但是,正如您所描述的那樣,似乎沒有簡單的方法來確定光標所在的單詞的開頭,因爲Document.ContentStart和Document.ContentEnd指向句子的開始和句子的結束。可能是我需要從TextPointer位置搜索空白空間。 (但後來我會與2字節的單詞拼寫,沒有空間來確定單獨的單詞) – 2012-07-31 04:04:07

+0

我想這個問題已經過了很長一段時間... http://stackoverflow.com/questions/10904614/extract-text -from-脫字符號位置的textarea – 2012-07-31 04:30:28

2

好吧,爲了解決這個問題,我蠻橫強迫它。

我用 curCaret.GetTextInRun(LogicalDirection.Backward)curCaret.GetTextInRun(LogicalDirection.Forward)

preCaretString.LastIndexOf(" ")postCaretString.IndexOf(" ")加上其他隔離分開的字一起,得到了子。

最後我添加了字符串的前半部分和字符串的後半部分以獲取當前的光標字。

我打賭有這樣的聰明的方式,但至少在這個問題解決了

2

附加此功能,任意RichTextBox的,現在被稱爲testRTB,並查看結果輸出窗口:

private void testRTB_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
     TextPointer start = testRTB.CaretPosition; // this is the variable we will advance to the left until a non-letter character is found 
     TextPointer end = testRTB.CaretPosition; // this is the variable we will advance to the right until a non-letter character is found 

     String stringBeforeCaret = start.GetTextInRun(LogicalDirection.Backward); // extract the text in the current run from the caret to the left 
     String stringAfterCaret = start.GetTextInRun(LogicalDirection.Forward);  // extract the text in the current run from the caret to the left 

     Int32 countToMoveLeft = 0; // we record how many positions we move to the left until a non-letter character is found 
     Int32 countToMoveRight = 0; // we record how many positions we move to the right until a non-letter character is found 

     for (Int32 i = stringBeforeCaret.Length - 1; i >= 0; --i) 
     { 
      // if the character at the location CaretPosition-LeftOffset is a letter, we move more to the left 
      if (Char.IsLetter(stringBeforeCaret[i])) 
       ++countToMoveLeft; 
      else break; // otherwise we have found the beginning of the word 
     } 


     for (Int32 i = 0; i < stringAfterCaret.Length; ++i) 
     { 
      // if the character at the location CaretPosition+RightOffset is a letter, we move more to the right 
      if (Char.IsLetter(stringAfterCaret[i])) 
       ++countToMoveRight; 
      else break; // otherwise we have found the end of the word 
     } 



     start = start.GetPositionAtOffset(-countToMoveLeft); // modify the start pointer by the offset we have calculated 
     end = end.GetPositionAtOffset(countToMoveRight);  // modify the end pointer by the offset we have calculated 


     // extract the text between those two pointers 
     TextRange r = new TextRange(start, end); 
     String text = r.Text; 


     // check the result 
     System.Diagnostics.Debug.WriteLine("[" + text + "]"); 
} 

將Char.IsLetter(...)更改爲Char.IsLetterOrDigit(...)或其他適當的選項,具體取決於是否希望保留數字。

提示:將其解壓縮到獨立程序集中的擴展方法中,以便在需要時訪問它。

0

由於單詞被空格分開,所以您可以遍歷插入符號周圍的遍歷,直到找到空格。即使您的RichTextBox甚至包含不同的字體和字體大小,此功能也可以正常工作。

public string GetWordByCaret(LogicalDirection direction) 
    { 
     // Get the CaretPosition 
     TextPointer position = this.CaretPosition; 
     TextPointerContext context = position.GetPointerContext(direction); 

     string text = string.Empty; 

     // Iterate through the RichTextBox based on the Start, Text and End of nearby inlines 
     while (context != TextPointerContext.None) 
     { 
      // We are only interested in the text here 
      //, so ignore everything that is not text 
      if (context == TextPointerContext.Text) 
      { 
       string current = position.GetTextInRun(direction); 

       // The strings appended based on whether they are before the caret or after it... 
       // And well...I love switches :) 
       switch (direction) 
       { 
        case LogicalDirection.Backward: 
         { 
          int spaceIndex = current.LastIndexOf(' '); 

          // If space is found, we've reached the end 
          if (spaceIndex >= 0) 
          { 
           int length = current.Length - 1; 

           if (spaceIndex + 1 <= length) 
           { 
            text = current.Substring(spaceIndex + 1, length - spaceIndex) + text; 
           } 

           return text; 
          } 

          else 
           text = current + text; 
         } 
         break; 

        default: 
         { 
          int spaceIndex = current.IndexOf(' '); 

          // If space is found, we've reached the end 
          if (spaceIndex >= 0) 
          { 
           int length = current.Length; 

           if (spaceIndex <= length) 
           { 
            text += current.Substring(0, spaceIndex); 
           } 

           return text; 
          } 

          else 
           text += current; 
         } 
         break; 
       } 
      } 

      // Move to the next position 
      position = position.GetNextContextPosition(direction); 

      // Get the next context 
      if (position != null) 
       context = position.GetPointerContext(direction); 
      else 
       context = TextPointerContext.None; 
     } 
     return text; 
    } 

現在你可以得到你喜歡這個字的意思了。

string before = GetWordByCaret(LogicalDirection.Backward); 
    string after = GetWordByCaret(LogicalDirection.Forward); 
    string word = before + after; // :) 
相關問題