Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent Results with RichTextBox ScrollToCaret

I am working with a RichTextBox in C#. It exists on a TabPage. When the TabPage is selected, I aim to populate the RichTextBox, and scroll to the end. I have tried the slight variations on solutions for this common question, the main one being along the lines of:

MyRichTextBox.Select(MyRichTextBox.Text.Length, 0);  
MyRichTextBox.ScrollToCaret();  

or:

MyRichTextBox.SelectionStart = MyRichTextBox.Text.Length;  
MyRichTextBox.ScrollToCaret();  

This is producing inconsistent results, albeit in a predictable manner. It will alternate between scrolling to the bottom, and scrolling one line short of the bottom. Respectively illustrated (sorry for the links, new user so I can't post the images):
Successfully scrolled to bottom
Scrolled to one line short of the bottom
I am surprised to find nothing mentioning this behaviour through my searches, and have decided to ask if anyone here has encountered this, and/or has a solution in mind. If it comes down to it, I suppose I can go with something along the lines of itsmatt's answer.

like image 679
art.vandelay.31415 Avatar asked Dec 16 '11 13:12

art.vandelay.31415


People also ask

How do I scroll the contents of a RichTextBox?

RichTextBox1.SelectedText = TextBox1.Text + " "; RichTextBox1.ScrollToCaret (); } } This method enables you to scroll the contents of the control until the caret is within the visible region of the control.

What is scroll to caret in text box?

Text Box Base. Scroll ToCaret Method System. Windows. Forms Scrolls the contents of the control to the current caret position.

What is the scrolltocaret method?

Scroll ToCaret Method System. Windows. Forms Scrolls the contents of the control to the current caret position. The following code example demonstrates how to use the Keys enumeration and the ScrollToCaret method to ensure that the text insertion point, represented by the caret, is always visible on the screen after the ENTER key has been pressed.

How do I run an example of RichTextBox from a form?

To run the example, paste the following code in a form containing a TextBox control called TextBox1 and a RichTextBox control called RichTextBox1. This example requires that the event-handling method has been associated with the KeyDown event.


2 Answers

I did some further experimentation with ScrollToCaret and it just does not end up in the same position every time. Since my goal is limited to only scrolling all the way to the bottom, it was then a good candidate for sending the WM_VSCROLL message (277, or 0x115) to the control, with wParam of SB_PAGEBOTTOM (7). This consistently scrolls all the way to the very bottom exactly like I needed:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
private const int WM_VSCROLL = 277;
private const int SB_PAGEBOTTOM = 7;

public static void ScrollToBottom(RichTextBox MyRichTextBox)
{
    SendMessage(MyRichTextBox.Handle, WM_VSCROLL, (IntPtr)SB_PAGEBOTTOM, IntPtr.Zero);
}
like image 121
art.vandelay.31415 Avatar answered Sep 28 '22 00:09

art.vandelay.31415


Change this to fit your working code..

String gotoCaret = "Something on this line.";
int position = textBox.Text.IndexOf(gotoCaret);
MyRichTextBox.SelectionStart = position;
MyRichTextBox.ScrollToCaret();
like image 23
MethodMan Avatar answered Sep 28 '22 02:09

MethodMan