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

时间:2011-03-10 10:28:02

标签: wpf richtextbox

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

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

感谢

4 个答案:

答案 0 :(得分:1)

请验证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++;
}

答案 1 :(得分:1)

获取行数的代码:

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

    rtb.CaretPosition = rtb.Document.ContentStart;

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

    return result;
}

答案 2 :(得分:0)

我找到了解决方案。也许有一种更简单的方法,如果是这样,请告诉我

    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;

    }

答案 3 :(得分:0)

我尝试了代码并且始终没有给出正确的结果。 一个聪明的方法就是这个

    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
                }
            }


    }