views:

332

answers:

2

I have a RichTextBox that I'm spewing log information to, but the RichTextBox seems to want to horizontally scroll whenever text is appended that is too long to fit.

After searching extensively, and repeatedly failing with ScrollToCaret(), it would seem that this function controls the vertical scroll position, but not the horizontal. I have also attempted at using the API calls GetScrollRange() and SetScrollPos(), and while these do place the scrollbars in the right locations, they aren't controlling the actual location at which the RichTextBox is scrolled.

+1  A: 

Could it be that there is no newline at the end of each log information in the log? use the System.Environment.Newline to append to the end of each line before putting it into the richtextbox. Take a look at this article on CodeProject which might help you and set you up in the right direction.

Hope this helps, Best regards, Tom.

tommieb75
Originally, when I was appending text I was doing Environment.NewLine + text. I switched it around, and ScrollToCaret() does the trick now.Granted this isn't as elegant as I'd like (the extra line at the bottom of the RTB really bothers me), but works without needing to worry about importing anything.Thanks!
MichaelC
A: 

This is a little messy, but it should work: When you do a ScrollToCaret and the horizontal scroll position changes, it fires an HScroll event. If the HScroll event occurs when you don't want it to, you can move the horizontal scroll bar back by 1. Move the selectionstart to 1 character after the previous crlf 2. Set the selectionstart to that location, and then 3. ScrollToCaret.

xpda