Resource icon

Automatically scroll textbox to bottom (vb.net)

When working with a multi-line text box you usually want to have the newest bit of text added to the box to be shown in the display area.

The easy way of doing that is to append your new text to the front of existing text. This way new text is added at the top of box instead of the bottom.
Code:
Me.TextBox1.Text = "Hello, world!" & vbCrLf & Me.TextBox1.Text
That works but might be confusing to the user. If you add it to the bottom of the box, though, the user would have to scroll down over & over again to see the new text. That is not desirable.

The answer is to have the text box scroll bar automatically moved to the bottom when you add new text. To do that, use the code below.
Code:
Me.TextBox1.Text = Me.TextBox1.Text & vbCrLf & "Hello, world!"
Code:
Private Sub TextBox1_Change() Handles TextBox1.TextChanged
    With Me.TextBox1
        .SelectionStart = Len(.Text)
        .ScrollToCaret()
    End With
End Sub
That selects the textbox selected text to be the very last character of your text box, using SelectionStart, and then moves to it using ScrollToCaret.

Don't forget to change "TextBox1" to the actual name of your textbox in your code. ;)
Author
Kevin
Views
134
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from Kevin

Back
Top