How to set the rich textbox to the last line when the text is changed

Recently I was working with Windows form and wanted the scroll of the rich textbox to be on the last line when there was some text changed. I am very new to windows form (having worked mainly in web forms [Asp.Net]).

Then I saw this property of the textbox called ScrollToCaret. So what I did was made the selection start at the end of the textbox and scroll the caret there.

this.txtMessages.SelectionLength = 0;
this.txtMessages.SelectionStart = this.txtMessages.Text.Length;
this.txtMessages.ScrollToCaret();

What I do here is unselect the richtextbox and then start the selection at the end of the textbox and scroll the caret there.

Thanks
Vikram

P.S. Used the above code in the TextChanged event of the Rich Textbox


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 11/30/2006 8:36:37 AM

Thanks, that's exactly what I needed today.

Zen

Posted on 4/28/2008 2:39:48 AM

It's so great. Thank you for you post. I'm stuck all day and finally find it out.

Posted on 8/4/2008 11:58:39 PM

Thanks a lot, it was so helpfull for me!!!

Posted on 8/4/2008 11:59:09 PM

Thanks a lot, it was so helpfull for me!!!

Posted on 10/30/2008 4:28:58 AM

simple but helpful

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia