简体   繁体   English

在RichTextBox中控制水平滚动

[英]Controlling horizontal scroll in RichTextBox

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. 我有一个RichTextBox,可以向其中发送日志信息,但是RichTextBox似乎想在附加了太长而无法容纳的文本时水平滚动。

After searching extensively, and repeatedly failing with ScrollToCaret() , it would seem that this function controls the vertical scroll position, but not the horizontal. 经过广泛搜索,并反复使用ScrollToCaret()失败,似乎此函数控制了垂直滚动位置,但没有控制水平滚动位置。 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. 我也尝试过使用API​​调用GetScrollRange()SetScrollPos() ,尽管它们确实将滚动条放置在正确的位置,但它们并没有控制RichTextBox滚动的实际位置。

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. 使用System.Environment.Newline附加到每行的末尾,然后再将其放入Richtextbox。 Take a look at this article on CodeProject which might help you and set you up in the right direction. 看一下CodeProject上的这篇文章 ,它可能会对您有所帮助,并朝着正确的方向发展。

Hope this helps, Best regards, Tom. 希望这对您有所帮助,汤姆,谢谢。

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. 这有点杂乱无章,但是它应该可以工作:当您执行ScrollToCaret并且水平滚动位置发生变化时,它将触发HScroll事件。 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. 如果您不希望发生HScroll事件,则可以将水平滚动条向后移动1。将selectionstart移到前一个crlf之后的1个字符。2将selectionstart设置到该位置,然后将3设置为ScrollToCaret。

Easy Way to Jump To Start of Last Line Of ReadOnly RichTextBox 简单的方法跳转到ReadOnly RichTextBox的最后一行的开始

Salvete! Salvete! Here is what I did. 这是我所做的。 I was using a richtext box as a logging display, so it was constantly receiving new strings appended to the bottom of the text, and the strings were always longer than the richtext box was wide. 我使用的是RTF文本框作为日志显示,因此它不断接收追加到文本底部的新字符串,并且这些字符串总是长于RTF文本框的宽度。 I wanted to have the last line always showing, but at the start of the line instead of the end. 我希望总是显示最后一行,但是要在行的开头而不是结尾。

I added a TextChanged event handler (you get it in the properties panel of the richtext box). 我添加了一个TextChanged事件处理程序(您可以在Richtext框的属性面板中找到它)。 The vbcrlf ensures that the last line is always a blank line, and since there is no text in that line, the ScrollToCaret() will automatically be at the first character. vbcrlf确保最后一行始终为空行,并且由于该行中没有文本,因此ScrollToCaret()将自动位于第一个字符处。 Unless you have textwrap turned on, make sure to set your scrollbar properties to both so users can scroll to see what is at the other end of the lines. 除非启用了textwrap,否则请确保将滚动条属性both设置为both以便用户可以滚动查看行另一端的内容。

Note that this solution only works for a read-only richtext box. 请注意,此解决方案仅适用于只读的 RTF文本框。 If it isn't read-only and the user types into it, every character will end up on its own line! 如果它不是只读的,并且用户输入了它,那么每个字符都将以自己的行结尾! With readonly set to true, your program will still be able to change the text - just the user won't be able to, and the last-entered line will always be the one visible! readonly设置为true,您的程序仍将能够更改文本-只是用户将无法更改文本,并且最后输入的行将始终是可见的!

 Private Sub my_richtextbox_TextChanged( sender As System.Object,  e As System.EventArgs) Handles my_richtextbox.TextChanged
    With my_richtextbox
        .Text = .Text & vbcrlf
        .SelectionStart = .Text.Length
        .ScrollToCaret()
    End With
End sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM