c# WPF文本框选择

本文关键字:选择 文本 WPF | 更新日期: 2023-09-27 17:54:53

我有一个RichTextBox,例如这段文字:

嗨,我的名字是{name}!

当我把光标放在括号之间时,我希望我的richtextbox能够选择括号和括号之间的整个单词。

所以当我这样做时:('|'是光标)

嗨,我的名字是{n|ame}!

我想选择'{name}'

我该怎么做?

c# WPF文本框选择

我这样做是为了扩展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 + "'");
    }
}

我写了一些你可以处理的东西(下面的代码只适用于单行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));
        }
    }
}