wpf richtextbox 检查插入符是否在最后一行或计算它有多少行

发布于 2024-10-21 19:23:18 字数 111 浏览 2 评论 0原文

我试图在富文本框中找出插入符是否位于最后一行。这可能吗?

注意:最后我还添加了:或计算它有多少行,这是因为在miscrosoft论坛中有一个用于检测插入符在哪一行的示例。

谢谢

I am trying to find out in a richtext box whether the caret is position in the last line. Is this possible?

NOTE: At the end I also added: or count how many lines it has, this is because in the miscrosoft forum there is an example for detecting in which line the caret is.

thanks

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

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

发布评论

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

评论(4

季末如歌 2024-10-28 19:23:18

请验证 msdn 链接

http:// Social.msdn.microsoft.com/Forums/en/wpf/thread/667b5d2a-84c3-4bc0-a6c0-33f9933db07f

如果您确实想知道插入符号的行号,您可以执行以下操作(可能需要一些调整):

TextPointer caretLineStart = rtb.CaretPosition.GetLineStartPosition(0);
TextPointer p = rtb.Document.ContentStart.GetLineStartPosition(0);
int caretLineNumber = 1;

while (true)
{
    if (caretLineStart.CompareTo(p) < 0)
    {
        break;
    }

    int result;
    p = p.GetLineStartPosition(1, out result);

    if (result == 0)
    {
        break;
    }

    caretLineNumber++;
}

Please verify the msdn link

http://social.msdn.microsoft.com/Forums/en/wpf/thread/667b5d2a-84c3-4bc0-a6c0-33f9933db07f

If you really wanted to know the line number of the caret, you could do something like the following (probably needs some tweaking):

TextPointer caretLineStart = rtb.CaretPosition.GetLineStartPosition(0);
TextPointer p = rtb.Document.ContentStart.GetLineStartPosition(0);
int caretLineNumber = 1;

while (true)
{
    if (caretLineStart.CompareTo(p) < 0)
    {
        break;
    }

    int result;
    p = p.GetLineStartPosition(1, out result);

    if (result == 0)
    {
        break;
    }

    caretLineNumber++;
}
梦年海沫深 2024-10-28 19:23:18

获取行数的代码:

Int32 CountDisplayedLines(RichTextBox rtb)
{
    Int32 result = -1;

    rtb.CaretPosition = rtb.Document.ContentStart;

    while (rtb.CaretPosition.GetLineStartPosition(++result) != null)
    {
    }

    return result;
}

The code to get the number of lines:

Int32 CountDisplayedLines(RichTextBox rtb)
{
    Int32 result = -1;

    rtb.CaretPosition = rtb.Document.ContentStart;

    while (rtb.CaretPosition.GetLineStartPosition(++result) != null)
    {
    }

    return result;
}
轻许诺言 2024-10-28 19:23:18

我找到了解决办法。也许有更简单的方法,如果有请告诉我

    private void OnHasRtbReachedEnd(System.Windows.Controls.RichTextBox rtb)
    {
        TextPointer pointer1 = rtb.CaretPosition;
        int iCurrentLine = GetLineNumber(rtb);
        rtb.CaretPosition = rtb.Document.ContentEnd;
        int iLastLine = GetLineNumber(rtb);

        if (iCurrentLine == iLastLine)
        {
            if (!_bIsRtbMovingUpDown)
                MoveToNextDataGridRow();
            _bIsRtbMovingUpDown= false;
        }
        else
        {
            _bIsRtbMovingUpDown= true;
        }
        rtb.CaretPosition = pointer1;
    }

//这段代码来自
//http://social.msdn .microsoft.com/Forums/en/wpf/thread/667b5d2a-84c3-4bc0-a6c0-33f9933db07f

    private int GetLineNumber(System.Windows.Controls.RichTextBox rtb)
    {
        TextPointer caretLineStart = rtb.CaretPosition.GetLineStartPosition(0);
        TextPointer p = rtb.Document.ContentStart.GetLineStartPosition(0);
        int caretLineNumber = 1;

        while (true)
        {
            if (caretLineStart.CompareTo(p) < 0)
            {
                break;
            }

            int result;
            p = p.GetLineStartPosition(1, out result);

            if (result == 0)
            {
                break;
            }

            caretLineNumber++;
        }
        return caretLineNumber;

    }

I have found a solution. Maybe there is a simpler way, if so please let me know

    private void OnHasRtbReachedEnd(System.Windows.Controls.RichTextBox rtb)
    {
        TextPointer pointer1 = rtb.CaretPosition;
        int iCurrentLine = GetLineNumber(rtb);
        rtb.CaretPosition = rtb.Document.ContentEnd;
        int iLastLine = GetLineNumber(rtb);

        if (iCurrentLine == iLastLine)
        {
            if (!_bIsRtbMovingUpDown)
                MoveToNextDataGridRow();
            _bIsRtbMovingUpDown= false;
        }
        else
        {
            _bIsRtbMovingUpDown= true;
        }
        rtb.CaretPosition = pointer1;
    }

//This code comes from
//http://social.msdn.microsoft.com/Forums/en/wpf/thread/667b5d2a-84c3-4bc0-a6c0-33f9933db07f

    private int GetLineNumber(System.Windows.Controls.RichTextBox rtb)
    {
        TextPointer caretLineStart = rtb.CaretPosition.GetLineStartPosition(0);
        TextPointer p = rtb.Document.ContentStart.GetLineStartPosition(0);
        int caretLineNumber = 1;

        while (true)
        {
            if (caretLineStart.CompareTo(p) < 0)
            {
                break;
            }

            int result;
            p = p.GetLineStartPosition(1, out result);

            if (result == 0)
            {
                break;
            }

            caretLineNumber++;
        }
        return caretLineNumber;

    }
荒人说梦 2024-10-28 19:23:18

我尝试了代码,但并不总是给出正确的结果。
一种聪明的方法是这样的

    int previousCursorPosition;
    private void RichTarget_KeyDown_1(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Up || e.Key == Key.Down)
        {
            Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;
            previousCursorPosition = rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart);
        }
    }
    private void RichTextBox_KeyUp_1(object sender, KeyEventArgs e)
    {

            if (e.Key == Key.Up)
            {
                Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;

                if (previousCursorPosition == rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart))
                {
                     //do your staff

                }

            }
            else if (e.Key == Key.Down)
            {
                Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;
                if (previousCursorPosition == rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart))
                {
                    //do your staff
                }
            }


    }

I tried the code and is not giving correct results always.
One smart way to do it is this

    int previousCursorPosition;
    private void RichTarget_KeyDown_1(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Up || e.Key == Key.Down)
        {
            Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;
            previousCursorPosition = rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart);
        }
    }
    private void RichTextBox_KeyUp_1(object sender, KeyEventArgs e)
    {

            if (e.Key == Key.Up)
            {
                Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;

                if (previousCursorPosition == rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart))
                {
                     //do your staff

                }

            }
            else if (e.Key == Key.Down)
            {
                Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;
                if (previousCursorPosition == rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart))
                {
                    //do your staff
                }
            }


    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文