时间:2019-03-17 标签:c#wpfrichtextboxselection

发布于 2024-11-05 10:24:04 字数 200 浏览 1 评论 0原文

我有一个 RichTextBox,其中包含例如这段文本:

嗨,我的名字是 {name}!

当我将光标放在括号之间时,我希望 Richtextbox 选择括号之间的整个单词以及括号。

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

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

我想选择“{name}”,

我该怎么做?

I have a RichTextBox with for example this piece of text:

Hi, my name is {name}!

When I put my cursor between the brackets I want my richtextbox to select the entire word between brackets and also the brackets.

so when I do this: ('|'is the cursor)

Hi, my name is {n|ame}!

I want to select '{name}'

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-11-12 10:24:04

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

I have made this to extend the selection in a WPF RichTextBox. I'm new to WPF so I don't know if it's the best way to do it.

    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 + "'");
    }
}
岁月无声 2024-11-12 10:24:04

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

I write something you can work on (the code below only works with a single line 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));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文