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:

  1. dosn't work :-(

    ReplyDelete
  2. Thanks, it's very useful

    ReplyDelete
  3. Thanks, it's very useful

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

    ReplyDelete
  5. working successfully, thx

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

    -NRK

    ReplyDelete
  7. Works, thx

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

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

    ReplyDelete
  10. 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

    ReplyDelete
  11. muito bom.. obrigado
    very good.. thanks xD

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

    ReplyDelete
  13. works well, thanks.

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

    Easy as that...
    >= .NET 3.0

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

    ReplyDelete
  15. Many thanks

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

    textbox1.ScrollToEnd();

    ReplyDelete
  17. thanks it is very help full

    ReplyDelete
  18. Thanks, it's very useful

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

    textBox.AppendText(myTextToAdd);

    ReplyDelete
  20. This is working but to avoid scrollbar flicker just use:

    textBox.AppendText(myTextToAdd);

    ReplyDelete
  21. 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

    ReplyDelete
  22. 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)

    ReplyDelete
  23. 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.

    ReplyDelete
  24. İ love you man :)

    ReplyDelete
  25. My textBox is named "inputText":

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

    ReplyDelete
  26. thank you very much

    - max

    ReplyDelete

Note: Only a member of this blog may post a comment.