Windows Develop Bookmark and Share   
 index > Windows Forms General > Logging application: Want my RichTextBox to not scroll all the time.
 

Logging application: Want my RichTextBox to not scroll all the time.

So I have this application that records logs sent from another one of my applications. To add a log to the RichTextBox I have
I use the .AppendText method. I also have the .HideSelection property set to false. Anyways, I'm pretty much pumping out a log message every frame and it just keeps scrolling like crazy to the bottom of the screen.

This is all fine except I want it so that when I click somewhere on the RichTextBox it stops scrolling. Logs will still get appended, but I don't want it to autoscroll.

Basically I want it to behave like the output window in VS2005. Any suggestions?

For the record, in my Log function I had


Code Snippet

myTextBox.SuspendLayout();



int initialSelStart = myTextBox.SelectionStart;
bool rescroll = ( initialSelStart == myTextBox.Text.Length );

myTextBox.AppendText( str );

if( rescroll )
{
myTextBox.Focus();
myTextBox.SelectionStart = myTextBox.Text.Length;
}
else
myTextBox.SelectionStart = initialSelStart;




myTextBox.ResumeLayout();


The problem with this, was that it was causing the application to be incredibly slow and unresponsive. I realize that I don't need SuspendLayout and ResumeLayout in there, but is the SelectionStart stuff really that slow?
MrBones  Monday, March 17, 2008 5:09 PM

I created a quick prototype to examine the problem, I managed to get the control to preserve the selection but as new lines where added to the view the selection moved to the visible top of the control, but the selection did stay there as more lines where added, it did not matter how many lines were selected either.

So in effect, apart from the selection not locking to the exact offset position I got part the way there.

I was doing all this using a timer set to a100ms interval to add lines using the AppendText method.

I tried using a StringBuilder to hold the text but that had the same result.

Eventually, I created a derrived control from a RichTextBox and implemented my own version of AppendText which removed the tendancy of the control to scroll down.

I think that is the way to go, I stopped the development as I am sure you'll figure the rest out, but the AppendText function is what to shoot for in my opinion.

Depending on the frequency of updates, there is an slight issue of flicker but again, with carefull management of the user selection I think you'll crack it easily (Use the MouseDown event to clear the selection)

Update:

I managed to fix the position exactly using the AutoScrollOffset rather than scrolling to the selection itself, it seemed more accurate to do so.

It does seem clear there needs to be some kind of buffer/queue that is on a seperate thread to pump new lines to control when it best suited to do so, allowing for smoother interaction by the user. By this I mean the control waits until the user has finished making a selection before new lines are added.

Doing all this on 100ms intervals without some kind of bufferdid create flicker on the selection but only slightly

яeverser  Tuesday, March 18, 2008 2:47 AM

I created a quick prototype to examine the problem, I managed to get the control to preserve the selection but as new lines where added to the view the selection moved to the visible top of the control, but the selection did stay there as more lines where added, it did not matter how many lines were selected either.

So in effect, apart from the selection not locking to the exact offset position I got part the way there.

I was doing all this using a timer set to a100ms interval to add lines using the AppendText method.

I tried using a StringBuilder to hold the text but that had the same result.

Eventually, I created a derrived control from a RichTextBox and implemented my own version of AppendText which removed the tendancy of the control to scroll down.

I think that is the way to go, I stopped the development as I am sure you'll figure the rest out, but the AppendText function is what to shoot for in my opinion.

Depending on the frequency of updates, there is an slight issue of flicker but again, with carefull management of the user selection I think you'll crack it easily (Use the MouseDown event to clear the selection)

Update:

I managed to fix the position exactly using the AutoScrollOffset rather than scrolling to the selection itself, it seemed more accurate to do so.

It does seem clear there needs to be some kind of buffer/queue that is on a seperate thread to pump new lines to control when it best suited to do so, allowing for smoother interaction by the user. By this I mean the control waits until the user has finished making a selection before new lines are added.

Doing all this on 100ms intervals without some kind of bufferdid create flicker on the selection but only slightly

яeverser  Tuesday, March 18, 2008 2:47 AM
Ooo, adjusting the AutoScrollOffset. Good thought. Thanks for the feedback, I'll give it a shot.
MrBones  Tuesday, March 18, 2008 5:38 PM

You can use google to search for other answers

Custom Search

More Threads

• Office 2003 look & feel - howto skin my own application?
• Mouse Leaving/Entering Bounds of Panel Covered in Other Controls?
• Looking for a LOGIN form example
• Adding items to toolstrip
• in a form i have six text boxes for auto fill
• Converting sql field type diary to textbox display
• referenced memory message
• Memory collects and doesn't let go...
• Interacting with other windows/apps?
• Help converting string array to Label Control array