How to Automatically Scroll to the Bottom of a Multiline Text Box

How do I automatically scroll to the bottom of a multiline text box?


At regular intervals, I am adding new lines of text to it. I would like the textbox to automatically scroll to the bottom-most entry (the newest one) whenever a new line is added.

If you use TextBox.AppendText(string text), it will automatically scroll to the end of the newly appended text. It avoids the flickering scrollbar if you're calling it in a loop.

It also happens to be an order of magnitude faster than concatenating onto the .Text property. Though that might depend on how often you're calling it; I was testing with a tight loop.


This will not scroll if it is called before the textbox is shown, or if the textbox is otherwise not visible (e.g. in a different tab of a TabPanel). See TextBox.AppendText() not autoscrolling. This may or may not be important, depending on if you require autoscroll when the user can't see the textbox.

It seems that the alternative method from the other answers also don't work in this case. One way around it is to perform additional scrolling on the VisibleChanged event:

textBox.VisibleChanged += (sender, e) =>
{
if (textBox.Visible)
{
textBox.SelectionStart = textBox.TextLength;
textBox.ScrollToCaret();
}
};

Internally, AppendText does something like this:

textBox.Select(textBox.TextLength + 1, 0);
textBox.SelectedText = textToAppend;

But there should be no reason to do it manually.

(If you decompile it yourself, you'll see that it uses some possibly more efficient internal methods, and has what seems to be a minor special case.)

autoscroll to bottom of multiline textbox being updated by backgroundworker

Try it this way:

'textbox.Text = text
textbox.AppendText(text)

The code you commented out wasn't running on the GUI thread, and as M Granja pointed out, AppendText will automatically scroll to the appended text in the box, so no need to call ScrollToCaret.

Autoscroll to the bottom of a multiline textbox

If you're using AppendText, you can completely get rid of that console_TextChanged method because AppendText already does this for you.

For some reason (may be a bug?) when the TextBox is not exposed to the screen, AppendText doesn't seem to scroll to end. I have no good explanation now, need to look into the .Net framework source.

As a workaround just move all your code to MyBase.Shown event, not Load event. That works as expected, difference is Shown event will be raised as soon as the Form is first rendered in the screen as opposed to Load which gets fired before the form is rendered.

How to make autoscroll multiline TextBox in WinForms?

Set the TextBox properties:

Multiline = True;
ScrollBars = Both;

To auto scroll on the TextChanged event:

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

How to constantly scroll to the end of text in multiline text box?

I'd say that when you refresh, you could move the selection cursor to the end, then scroll the textbox 'til it's visible using ScrollToCaret.

That'll be something like

 yourtextbox.SelectionStart = yourtextbox.Text.Length
yourtextbox.ScrollToCaret()

Multiline Textbox with automatic vertical scroll

According to this question: TextBox.ScrollToEnd doesn't work when the TextBox is in a non-active tab

You have to focus the text box, update the caret position and then scroll to end:

Status.Focus();
Status.CaretIndex = Status.Text.Length;
Status.ScrollToEnd();

EDIT

Example TextBox:

<TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" 
AcceptsReturn="True" Name="textBox"/>

Auto scroll to bottom with a textbox

You can do it via a call to a sub;

AppendText "Bla de bla bla."
.
.
sub AppendText(strText As String)
with txtStatus
.setfocus '//required
.value = .value & strText & vbNewLine
.selstart = len(.Value)
end with
end sub

Automatically scroll multiline TextFormField when it extends the maxLines attribute

This appears to be a missing feature in the Flutter Framework, I've filed a bug to get it resolved: https://github.com/flutter/flutter/issues/9365



Related Topics



Leave a reply



Submit