'RichTextBox auto scroll to bottom when replacing a line

I'm using a richtexbox to put logs for an application of mine. I'm also using it as some sort of progress bar for a background worker.

I'm not going to go in to detail but the application loops through several devices and gather data, the current data being processed is displayed on the last line of the richtextbox and is replaced every time a new one is processed.

Here's a small sample of the log.

Found 239 record(s), Transferred 239 record(s) from: 10.10.10.10 - 10/24/2016 7:37:45 PM
Found 42 record(s), Transferred 42 record(s) from: 10.10.10.11 - 10/24/2016 7:37:58 PM
43593...

The last line would be replaced by some other data while processing. Until it's finished and replaced again like this.

Found 239 record(s), Transferred 239 record(s) from: 10.10.10.10 - 10/24/2016 7:37:45 PM
Found 42 record(s), Transferred 42 record(s) from: 10.10.10.11 - 10/24/2016 7:37:58 PM
Found 2 record(s), Transferred 2 record(s) from: 10.10.10.12 - 10/24/2016 7:38:03 PM
986035...

I add data to the richtextbox using AppendText, this works fine and scrolls the richtextbox to the bottom when I add new records. But for my "progress bar" I replace the last line like this:

Me.rtbLogs.Rtf = Me.rtbLogs.Rtf.Replace(Me.rtbLogs.Lines(rtbLogs.Lines.Length - 1), e.UserState.ToString())

Everything works fine except while it's replacing texts the richtextbox scrolls back to the top.



Solution 1:[1]

You need to scroll to the end of the text. Try this:

  ' Append the new text and a new line
 RichTextbox1.AppendText("New Text" & vbNewLine)
  ' Sets the starting point of the selection         
RichTextbox1.SelectionStart = Len(RichTextbox1.Text)
  ' Scrolls to the caret
RichTextbox1.ScrollToCaret()
  ' Select the range 
RichTextbox1.Select()

Solution 2:[2]

To jump to the end of richtextbox: RichTextBox1.text.Focus() SendKeys.Send("^{END}")

'without focus the cursor moves there but it won't scroll down

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neelix
Solution 2 YaoChen Aan