防止 RichTextBox 自动滚动
我有一个使用 RichTextBox 控件实现的只读数据记录窗口。 我希望能够禁用用户单击控件时发生的自动滚动,以便用户可以选择特定日志进行复制/粘贴操作或其他操作。 但是,一旦用户单击 RichTextBox,它就会自动滚动到底部,这使得这变得很困难。
有人知道一种方法来覆盖这种行为吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果未隐藏所选内容,RichTextBox 控件会自动滚动到当前所选内容。 RichTextBox.AppendText() 除了附加文本之外,还修改当前选择,因此间接触发“自动滚动”行为。 请注意,如果 RichTextBox.HideSelection 设置为 true,则当控件未处于焦点时,所选内容将被隐藏; 这解释了您所描述的行为,其中仅当用户单击控件时才会发生自动滚动。 (从而给予它焦点)
为了防止这种情况,您需要在附加文本时执行以下操作:
您可能还想检查选择是否已经存在在文本末尾,并允许自动滚动行为(如果是)- 这本质上模拟了 Visual Studio 输出窗口的行为。 例如:
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:
您可能会看看做这样的事情:
然后在您附加日志数据的方法中(我在这里做了一些假设)您可能会做这样的事情:
我做了一个带有计时器的小测试应用程序,该计时器在richtextbox 它阻止了它的滚动,这样我就可以进行文本选择。 它存在一些位置问题并且并不完美,但也许它会帮助您找到自己的解决方案。
一切顺利!
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 的解决方案有一个问题,当“附加”某些文本时,滚动条会移动,使得选择内容转到面板顶部。
解决方案是保存/恢复滚动位置:
这个解决方案对我来说更完整。
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.