开发者

wpf richtextbox check if caret is in the last line or count how many lines it has

开发者 https://www.devze.com 2023-02-15 19:33 出处:网络
I am trying to find out in a richtext box whether the caret 开发者_JAVA技巧is position in the last line. Is this possible?

I am trying to find out in a richtext box whether the caret 开发者_JAVA技巧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


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


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


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;

    }


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


    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号