如何从 Silverlight 4 RichTextBox 控件获取当前行文本到光标

发布于 2024-09-13 01:36:10 字数 1054 浏览 0 评论 0原文

在 Winforms RichTextBox 控件中,我以前使用过 GetLineFromCharIndex 方法和 GetFirstCharIndexOfCurrentLine 来计算当前行上键入文本的起点和终点。

我正在努力使用 Silverlight 4 中的新 RichTextBox 控件,因为似乎没有等效的方法。 GetPositionFromPoint 可用,但看起来很笨重。

干杯。

更新了...我已经采取了一些措施来完成这项工作,但这需要我使用控件的 Select 方法,这感觉非常错误...

private string GetCurrentLine()
{
    TextPointer prevSelStart = richTextBox1.Selection.Start;
    Point lineStart = new Point(0, prevSelStart.GetCharacterRect(LogicalDirection.Forward).Y);
    TextPointer prevSelEnd = richTextBox1.Selection.End;
    TextPointer currentLineStart = richTextBox1.GetPositionFromPoint(lineStart);

    //need to find a way to get the text between two textpointers
    //other than performing a temporary selection in the rtb
    richTextBox1.Selection.Select(currentLineStart, prevSelStart);
    string text = richTextBox1.Selection.Text;
    //revert back to previous selection
    richTextBox1.Selection.Select(prevSelStart, prevSelEnd);

    return text;
}

In the Winforms RichTextBox control I have previously used the GetLineFromCharIndex method and the GetFirstCharIndexOfCurrentLine to work out the start and end points of the typed text on he current line.

I am struggling with the new RichTextBox control in Silverlight 4 as there doesn't appear to be equivalent methods. GetPositionFromPoint is available but seems a but clunky.

Cheers.

Updated...I have gone someway to making this work but this requires me to use the Select method of the control, this feels very wrong...

private string GetCurrentLine()
{
    TextPointer prevSelStart = richTextBox1.Selection.Start;
    Point lineStart = new Point(0, prevSelStart.GetCharacterRect(LogicalDirection.Forward).Y);
    TextPointer prevSelEnd = richTextBox1.Selection.End;
    TextPointer currentLineStart = richTextBox1.GetPositionFromPoint(lineStart);

    //need to find a way to get the text between two textpointers
    //other than performing a temporary selection in the rtb
    richTextBox1.Selection.Select(currentLineStart, prevSelStart);
    string text = richTextBox1.Selection.Text;
    //revert back to previous selection
    richTextBox1.Selection.Select(prevSelStart, prevSelEnd);

    return text;
}

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

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

发布评论

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

评论(2

穿透光 2024-09-20 01:36:10

我认为您不能避免选择,这是一种正确的方法(“选择”只是一个逻辑选择),但您可以使用 TextPointer 避免 GetPositionFromPoint 。 GetNextInsertionPosition(LogicalDirection ):从 richTextBox1.Selection.Start 开始并向行首移动 (char != '\n')

I don't think you can't avoid the selection, it's a proper way to do it (the "selection" is just a logical one), but you can avoid GetPositionFromPoint with TextPointer.GetNextInsertionPosition(LogicalDirection ): Start from richTextBox1.Selection.Start and move towards the beginning of the line (char != '\n')

夏花。依旧 2024-09-20 01:36:10

我需要弄清楚我何时处于 RTB 的顶线或底线。为此,我使用了 GetCharacterRect 方法,然后比较顶部以查看它是在最后一行还是第一行。

您可以执行相同的操作,并使用文本指针在文本中移动以及顶部不匹配的次数。

这是查看光标是否位于第一行或最后一行的代码:

    private bool IsCursorOnFirstLine()
    {
        TextPointer contentStart = this.ContentStart;
        TextPointer selection = this.Selection.End;
        Rect startRect = contentStart.GetCharacterRect(LogicalDirection.Forward);
        Rect endRect = selection.GetCharacterRect(LogicalDirection.Forward);
        return startRect.Top == endRect.Top;
    }

    private bool IsCursorOnLastLine()
    {
        TextPointer start = this.Selection.Start;
        TextPointer end = this.ContentEnd;
        Rect startRect = start.GetCharacterRect(LogicalDirection.Forward);
        Rect endRect = end.GetCharacterRect(LogicalDirection.Backward);
        return startRect.Top == endRect.Top;
    }

I've needed to figure out when I was on the top line or bottom line of the RTB. To do this I used the GetCharacterRect methods then compared the tops to see if it was on the last line or first line.

You could do the same and use a text pointer to move through the text and number of times the tops don't match.

Here's code to see if the cursor is on the first or last lines:

    private bool IsCursorOnFirstLine()
    {
        TextPointer contentStart = this.ContentStart;
        TextPointer selection = this.Selection.End;
        Rect startRect = contentStart.GetCharacterRect(LogicalDirection.Forward);
        Rect endRect = selection.GetCharacterRect(LogicalDirection.Forward);
        return startRect.Top == endRect.Top;
    }

    private bool IsCursorOnLastLine()
    {
        TextPointer start = this.Selection.Start;
        TextPointer end = this.ContentEnd;
        Rect startRect = start.GetCharacterRect(LogicalDirection.Forward);
        Rect endRect = end.GetCharacterRect(LogicalDirection.Backward);
        return startRect.Top == endRect.Top;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文