2011-05-04 148 views
0

我有例如此一段文字,一個RichTextBox:C#WPF RichTextBox的選擇

嗨,我的名字是{NAME}!

當我將光標放在方括號之間時,我想讓我的richtextbox選擇括號和括號之間的整個單詞。

所以當我這樣做:( '|' 是光標)

嗨,我的名字是{N | AME}!

我想選擇'{name}'

我該怎麼做?

回答

0

我寫的東西,你可以工作(下面的代碼只適用於單行RTB):

private void richTextBox1_PreviewMouseUp(object sender, MouseButtonEventArgs e) 
{ 
    TextPointer oldpointer = richTextBox1.CaretPosition; //current caret position 
    int startposition = richTextBox1.Document.ContentStart.GetOffsetToPosition(richTextBox1.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward)); 

    if (startposition > 2) 
    { //get RTB text  
     richTextBox1.SelectAll(); 
     string wholetext = richTextBox1.Selection.Text; 

     //reset the caret back 
     richTextBox1.CaretPosition = oldpointer; 

     //split text by the caret 
     string starthalf = wholetext.Substring(0, startposition - 2); 
     string endhalf = wholetext.Remove(0, startposition - 2); 

     //get position of "{" and "}" 
     int seleStart = starthalf.LastIndexOf('{'); 
     int seleEnd = endhalf.IndexOf('}') < 0 ? -1 : endhalf.IndexOf('}') + starthalf.Length + 1; 

     //select the pattern 
     if (seleStart >= 0 && seleEnd > 0) 
     { 
      richTextBox1.Selection.Select(richTextBox1.Document.ContentStart.GetPositionAtOffset(seleStart + 2), richTextBox1.Document.ContentStart.GetPositionAtOffset(seleEnd + 2)); 
     } 
    } 
} 
2

我有此擴展在WPF RichTextBox的選擇。我是WPF的新手,所以我不知道這是否是最好的方式。

private TextRange ExtendSelection(LogicalDirection direction) 
{ 
    TextRange tr = new TextRange(CaretPosition, CaretPosition.GetInsertionPosition(direction)); 
    bool found = false; 
    while (!found) 
    { 
     if (tr == null) 
     { 
      break; 
     } 
     else 
     { 
      // If we are not at the end of the document (or at the beginning) 
      TextPointer next = null; 
      if (LogicalDirection.Forward.CompareTo(direction) == 0 && tr.End.CompareTo(Document.ContentEnd) == -1) 
      { 
       next = tr.End.GetNextInsertionPosition(direction); 
      } 
      else if (LogicalDirection.Backward.CompareTo(direction) == 0 && tr.Start.CompareTo(Document.ContentStart) == 1) 
      { 
       next = tr.Start.GetNextInsertionPosition(direction); 
      } 

      // Be careful with boundaries! 
      if (next != null) 
      { 
       TextRange trNext = new TextRange(CaretPosition, next); 
       char[] text = trNext.Text.ToCharArray(); 
       for (int i = 0; i < text.Length; i++) 
       { 
        if (Char.IsWhiteSpace(text[i]) || Char.IsSeparator(text[i])) 
        { 
         found = true; 
         break; 
        } 
       } 
       if (!found) 
       { 
        tr = trNext; 
       } 
      } 
      else 
      { 
       break; 
      } 
     } 
    } 
    return tr; 
} 

private void MyRichTextBox_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    TextRange left = ExtendSelection(LogicalDirection.Backward); 
    TextRange right = ExtendSelection(LogicalDirection.Forward); 
    if (!left.IsEmpty && !right.IsEmpty) 
    { 
     Selection.Select(left.Start, right.End); 
     Console.WriteLine("Highlight: '" + Selection.Text + "'"); 
    } 
}