在WPF RichTextBox控件中,获取单词游标的方法是打开的



我想知道如何在WPF RichTextBox中获取当前游标所在的单词。 我知道RichTextBox具有选择属性。 但是,这只会给我在RichTextBox中突出显示的文本。 相反,我想知道即使整个单词没有突出显示,光标所在的单词也是如此。

任何提示都表示赞赏。

非常感谢你。


将此函数附加到一个任意的RichTextBox,现在称为testRTB,并查看Output窗口获取结果:

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(...)或其他适当的选项,具体取决于是否希望保留数字。

提示:将其解压缩到单独程序集中的扩展方法中,以便在需要时访问它。


好吧,为了解决这个问题,我蛮横地逼着它。

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

以及preCaretString.LastIndexOf(" ")postCaretString.IndexOf(" ")加上其他分隔postCaretString.IndexOf(" ")来区分单词并获取子字符串。

最后,我添加了字符串的前半部分和字符串的后半部分以获取当前被光标化的单词。

我敢打赌,有这样做的更聪明的方式,但至少这解决了这个问题


您可以通过CaretPosition获取光标的当前位置。

不幸的是,没有简单的方法将字符放到插入位置的左侧/右侧。 我知道从RichTextBox中获取文本的唯一方法就是在这个答案中,这有点复杂。 但它会完成必要的。

链接地址: http://www.djcxy.com/p/61123.html

上一篇: Way to obtain the word cursor is on, in WPF RichTextBox Control

下一篇: Using GetLineStartPosition to get the end of a line in WPF RichTextBox