I have a readonly data logging window that I implemented using the RichTextBox control. I'd like to be able to disable the autoscrolling that happens when the user clicks in the control so that the user can select a specific log for copy/paste operations or whatever. However, as soon as the user clicks in the RichTextBox, it automatically scrolls to the bottom, making this difficult.
Anyone know a way to override this behavior?
Thanks!
The RichTextBox control automatically scrolls to the current selection, if the selection is not hidden. RichTextBox.AppendText(), in addition to appending text, also modifies the current selection, and so indirectly triggers the "autoscrolling" behaviour. Note that if RichTextBox.HideSelection is set to true, then the selection would be hidden when the control is not in focus; this explains the behaviour you described, where autoscrolling occurs only when the user clicks in the control. (thereby giving it focus) To prevent this, you need to do the following when appending text:
You may also want to check whether the selection is already at the end of the text, and allow the autoscrolling behaviour if it is - this essentially emulates the behaviour of Visual Studio's Output window. For example:
You might take a look at doing something like this:
then in your method that appends log data (I'm making some assumptions here) you might do something like this:
I did a little test app with a timer that did the append on the richtextbox and it stopped it from scrolling so I could do the text selection. It has some positional issues and isn't perfect, but perhaps it'll help move you toward a solution of your own.
All the best!
SytS's solution has an issue, when some text is "appended", the scroll bar moves such that the selection go to the top of the panel. A solution is to save/restore the scroll position with :
This solution is more complete for me.