0

I already used the below code. It moves current caret position in viewport top.

FrameworkContentElement fce = (tpNextLine2.Parent as FrameworkContentElement);
if (fce != null)
{
    fce.BringIntoView();
}

I want to move current caretposition textpointer in richtextbox viewport end using WPF.

How can I achieve this?

B.K.
  • 9,982
  • 10
  • 73
  • 105
arjun sanju
  • 135
  • 1
  • 1
  • 12

2 Answers2

1

If you don't want to change current caret position but scroll the WPF RichTextBox to move this position to the bottom of the visible area, you can use this code:

Rect rc = rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward);
rtb.ScrollToVerticalOffset(rc.Bottom + rtb.VerticalOffset - rtb.ViewportHeight);
Fratyx
  • 5,717
  • 1
  • 12
  • 22
  • @ Fratyx : Thank u. its like what i want.but how to check currentcaretposition is in viewport or not? Because after one pageup,each line scrolled.so i think,once i call your codes after check whether the current caret position is not in the viewport. – arjun sanju Jan 09 '16 at 05:26
  • 1
    @arjun sanju: You can check if a position is visible or not by [this code](http://stackoverflow.com/a/34691469/3135228). – Pollitzer Jan 10 '16 at 07:49
  • @ Crawumbo : The above same logic, how to get rc.Top in view? – arjun sanju Jan 11 '16 at 05:28
  • 1
    @arjun sanju: You can combine CaretPosition.GetCharacterRect, VerticalOffset, ViewportHeight and ExtentHeight to all possible calculations. E.g. if the CaretPosition (i.e. CharacterRect) is negative, the Caret is above the Viewport. If the position is bigger than ViewportHeight, the Caret is below the Viewport. – Fratyx Jan 11 '16 at 05:50
  • @ Fratyx : Can u show in the code for Rect caretposition in top of the viewport? – arjun sanju Jan 11 '16 at 10:16
  • 1
    `rtb.ScrollToVerticalOffset(rc.Top + rtb.VerticalOffset);` – Fratyx Jan 11 '16 at 10:41
  • @Fratyx : Sorry.not working.it also panning after away from viewport.I mean,read one word from hidden area.then only page up happened. – arjun sanju Jan 11 '16 at 11:51
  • @arjun sanju: Sorry, don't understand your intention. – Fratyx Jan 11 '16 at 13:09
  • @ Fratyx : Actually BringIntoView() working once move to invisible area firstword then only page down happened,same as our code rtb.ScrollToVerticalOffset(rc.Top + rtb.VerticalOffset); working.but i want whether the caretposition try to move invisible area,need to show that line in top of the viewport.Got my point? – arjun sanju Jan 12 '16 at 05:12
  • 1
    @arjun sanju: So you want to call this piece of code after the user scrolled the viewport or changed the caret position? – Fratyx Jan 12 '16 at 09:15
  • @ Fratyx : Yes.Your rtb.ScrollToVerticalOffset(rc.Top + rtb.VerticalOffset); is working.but only works when the caretposition moved under viewport.i mean the first word of the line not shown,once moved to the second word then only the panning happened. – arjun sanju Jan 12 '16 at 14:45
0

Try this code :

RichTextBox1.Select(RichTextBox1.Text.Length - 1, 0);

RichTextBox1.ScrollToCaret();
Nakul Chaudhary
  • 25,572
  • 15
  • 44
  • 47