The Rich Text window scrolls down when new data is written to it.

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

Even if you scroll down the page as soon as new data is written, it will return up. I would like the opposite.

So, when new data is written, it stays down. How can i do this?

+59
c # winforms richtextbox
Feb 23 '12 at 16:01
source share
7 answers

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(); } 
+124
Feb 23 '12 at 18:11
source share
โ€” -

A RichTextBox will scroll to the end if it has focus, and you use AppendText to add information. If you set HideSelection to False, it will retain its choice when it loses focus and will automatically scroll.

I developed a GUI Log Viewer that used the method below. He used to the full core. Get rid of this code and set HideSelection to False, getting the processor up to 1-2%

 //Don't use this! richTextBox.AppendText(text); richTextBox.ScrollToEnd(); 
+13
Jan 31 '14 at 21:39
source share
  [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);

using the above method you can scroll the text field down

+4
May 08 '18 at 15:04
source share

I will do it simply:

  • Set HideSelection property to false

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

The 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); } 
0
Apr 05 '19 at 4:05
source share

When writing new data, if you use AppendText() , it will not scroll up and will always stay below.

-2
May 14 '13 at 5:44
source share

Pour un dรฉfilement progressif par exampleple โ†’

 namespace SongTabs { public class RichTextBoxAutoScroll { protected int LineJump { get; set; } protected int ActualLine = 1; Timer Timer { get; set; } RichTextBox RichTextBox { get; set; } public RichTextBoxAutoScroll(RichTextBox rtb,int speed,int linejump) { this.LineJump = linejump; this.RichTextBox = rtb; this.Timer = new Timer(); this.Timer.Interval = speed; this.Timer.Tick += Timer_Tick; } void Timer_Tick(object sender, EventArgs e) { RichTextBox.SelectionStart = RichTextBox.GetFirstCharIndexFromLine(ActualLine); RichTextBox.ScrollToCaret(); ActualLine += LineJump; } public void Start() { Timer.Start(); } } } 
-2
Oct 22 '15 at 4:03
source share

This is an old question, but I had this problem and I used the richTextBox_TextChanged event as above which works. But I believe this is a workaround and wants to document the actual solution if someone else is looking for it.

If you add it, it will automatically scroll, however the RichTextBox should be focused. So call Focus before the AppendText to make sure it scrolls automatically.

 richTextBox.Focus(); richTextBox.AppendText(text); 
-2
Nov 18 '16 at 18:00
source share



All Articles