28 November 2008

C# Textbox Auto Scroll To End ScrollToCaret Scrolling Textbox

Many of my sample C# codes,
I use textbox to write the results.

When textbox is multiline property set to true, and when I insert text into it,
I want it to auto scroll to the last line.

Here is a simple way to auto scrolling textbox.
 textbox1.SelectionStart = textbox1.Text.Length;
textbox1.ScrollToCaret();
textbox1.Refresh();
Textbox SelectionStart will force the textbox control to select the last part of the text,
and ScrollToCaret() function will auto scroll textbox to the end.

29 comments:

Anonymous said...

dosn't work :-(

Anonymous said...

Thanks, it's very useful

Anonymous said...

Thanks, it's very useful

artcoding said...

Does not work if your TextBox is on another tab ( when using a TabControl )

Anonymous said...

working successfully, thx

Rajat Swarup said...

Awesome solution!

Anonymous said...

It works for me. Thanks a lot for putting the solution in such a simple form!

-NRK

Anonymous said...

Works, thx

Marcus Nyberg said...

Thanks. It worked!

Anonymous said...

TextBox1.AppendText() works fater, not blinking and scroll text.

Mystery said...

TextBox.AppendText() works awesome, and requires just 1 line of code! Thanks a lot!

Anonymous said...

at least for me this line was unnecessary:

textbox1.SelectionStart = textbox1.Text.Length;

Somebody suggested TextBox1.AppendText()

but I'm not sure it is designed for this purpose

have to test it and check if there are some drawbacks

Leo said...

muito bom.. obrigado
very good.. thanks xD

Leo said...

muito bom.. obrigadoo
very good.. thanks ;D

Leo said...

thx =)

Anonymous said...

works well, thanks.

C. van Dijk said...

tbPositionCursor.Select(tbPositionCursor.Text.Length, 0);

Easy as that...
>= .NET 3.0

Source: http://msdn.microsoft.com/en-us/library/ms752349.aspx

Anonymous said...

Many thanks

Anonymous said...

You can also use the built-in function for this..

textbox1.ScrollToEnd();

akkhan said...

thanks it is very help full

akkhan said...

Thanks, it's very useful

Anonymous said...

This is working but to avoid scroll bar flicker just use:

textBox.AppendText(myTextToAdd);

Anonymous said...

This is working but to avoid scrollbar flicker just use:

textBox.AppendText(myTextToAdd);

Anonymous said...

textBox.AppendText(myTextToAdd);
Will give error "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: chunkLength" when myTextToAdd has large text... i.e. 2000 lines

Anonymous said...

There is no AppendText, nor ScrollToEnd on Compact Framework, Select doesn't seem to work, and authors proposition causes flickering of scrollbar :P
(GODDAMN CAPTCHA, 7th try)

rbaleksandar said...

Thanks a lot! This really helped me. It's strange that I wasn't able to find anything like this offered in Visual Studio 2008. Would be nice to have it in the properties menu of the text box.

Anonymous said...

İ love you man :)

Anonymous said...

My textBox is named "inputText":

inputText.AppendText(charTxt);
inputText.Select(inputText.Text.Length, 0);
inputText.Focus();

Anonymous said...

thank you very much

- max