How do I scroll a RichTextBox to the bottom?

C#WinformsScrollRichtextbox

C# Problem Overview


I need to be able to scroll a RichTextBox to the bottom, even when I am not appending text. I know I can append text, and then use that to set the selection start. However I want to ensure it is at the bottom for visual reasons, so I am not adding any text.

C# Solutions


Solution 1 - C#

You could try setting the SelectionStart property to the length of the text and then call the ScrollToCaret method.

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

Solution 2 - C#

The RichTextBox will stay scrolled to the end if it has focus and you use AppendText to add the information. If you set HideSelection to false it will keep its selection when it loses focus and stay auto-scrolled.

I designed a Log Viewer GUI that used the method below. It used up to a full core keeping up. Getting rid of this code and setting HideSelection to false got the CPU usage down to 1-2%.

//Don't use this!
richTextBox.AppendText(text);  
richTextBox.ScrollToEnd();

Solution 3 - C#

In WPF you can use ScrollToEnd:

richTextBox.AppendText(text);  
richTextBox.ScrollToEnd();

Solution 4 - C#

Code should be written in the rich text box's TextChanged event like :

private void richTextBox_TextChanged(object sender, EventArgs e) {
       richTextBox.SelectionStart = richTextBox.Text.Length;
       richTextBox.ScrollToCaret();
}

Solution 5 - C#

There is no need for:

richTextBox.SelectionStart = richTextBox.Text.Length;

This does the trick:

richTextBox.ScrollToCaret();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVery Very CherryView Question on Stackoverflow
Solution 1 - C#BrandonView Answer on Stackoverflow
Solution 2 - C#DrWuView Answer on Stackoverflow
Solution 3 - C#mxgg250View Answer on Stackoverflow
Solution 4 - C#agileDevView Answer on Stackoverflow
Solution 5 - C#Ben RichardsView Answer on Stackoverflow