Rich Text box scroll to the bottom when new data is written to it

C#WinformsRichtextbox

C# Problem Overview


My program calls Java and then redirects stdout to a RichTextBox. My problem is that the vertical scrollbar always stays at the top of the box every time data is written.

Even if you scroll to the bottom, once new data has been written it goes to the top. I would like the opposite.

So when new data is written, it stays at the bottom. How can I do this?

C# Solutions


Solution 1 - C#

Yes, you can use the ScrollToCaret() method:

// bind this method to its TextChanged event handler:
// richTextBox.TextChanged += richTextBox_TextChanged;
private void richTextBox_TextChanged(object sender, EventArgs e) {
   // set the current caret position to the end
   richTextBox.SelectionStart = richTextBox.Text.Length;
   // scroll it automatically
   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#

I'll keep it simple:

  • Set HideSelection property to false

  • Use AppendText() method to add text to RichTextBox.

Code:

RichTextBox rtbTest;

void InitRichTextBox()
{
	//Init rtbTest...
	
	rtbTest.HideSelection = false;//Hide selection so that AppendText will auto scroll to the end
}

void AddText(string txt)
{
	rtbTest.AppendText(txt);
}

Solution 4 - C#

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

    internal static void ScrollToBottom(RichTextBox richTextBox)
    {
        SendMessage(richTextBox.Handle, WM_VSCROLL, (IntPtr)SB_PAGEBOTTOM, IntPtr.Zero);
        richTextBox.SelectionStart = richTextBox.Text.Length;
    }

ScrollToBottom(richTextBox);

by using above method you can scroll rich text box to bottom

Solution 5 - C#

When writing new data, if you use AppendText() it wont scroll up and will always stay at the bottom.

Solution 6 - C#

This is an old question, but I had this problem and I used the richTextBox_TextChanged event as above, which works. But I feel this is a workaround and wanted to document the actual solution in case anybody else looks for it.

If you append it will auto-scroll, however the RichTextBox has to be focused. So call Focus before AppendText to make sure it auto-scrolls.

richTextBox.Focus();

richTextBox.AppendText(text);

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
Questionuser1158745View Question on Stackoverflow
Solution 1 - C#OmarView Answer on Stackoverflow
Solution 2 - C#DrWuView Answer on Stackoverflow
Solution 3 - C#123iamkingView Answer on Stackoverflow
Solution 4 - C#Prem Kumar BadriView Answer on Stackoverflow
Solution 5 - C#Gayan DasanayakeView Answer on Stackoverflow
Solution 6 - C#DocWhoView Answer on Stackoverflow