简体   繁体   English

RichTextBox滚动

[英]RichTextBox scroll

I'm not sure what is going on with a RichTextBox in a Visual Studio C# windows application. 我不确定Visual Studio C#Windows应用程序中RichTextBox的状态。 This is not a web app. 这不是网络应用。

I have a procedure that processes several files. 我有一个处理几个文件的程序。 As it processes, it posts progress reports in the RichTextBox using AppendText. 在处理过程中,它将使用AppendText在RichTextBox中发布进度报告。 (The additions have newline chars which keep things neat.) This procedure was migrated from one app to another. (增加了换行符,使内容保持整洁。)此过程已从一个应用程序迁移到另一个应用程序。 The form in the new app was created from scratch with a new RichTextBox, and the code was copied. 新应用程序中的表单是使用新的RichTextBox从头开始创建的,代码已被复制。

On the original app, when the messages reach the bottom of the box, they begin to scroll so the new line of text is always visible. 在原始应用程序上,当消息到达框的底部时,它们开始滚动,因此新的文本行始终可见。 On the new app, it does not scroll (though I want it to) so the new messages (including the last one that indicates the process is done) are out of sight below the bottom. 在新的应用程序上,它不会滚动(尽管我希望它滚动),因此新消息(包括指示该过程已完成的最后一条消息)在底部下方看不见。 I've compared the two programs, and I don't see the difference. 我已经比较了两个程序,但看不到区别。 I don't see how this behavior would be controlled. 我不知道如何控制这种行为。 It is possible that there is a difference in the procedure that affects focus, or refresh behavior, or whatever. 该过程可能会有所不同,从而影响焦点,刷新行为等。

Where should I look? 我应该去哪里看?

richTextBox1.Select(richTextBox1.TextLength, 0); // put caret at end
richTextBox1.ScrollToCaret();

if you want to keep the selection you need to store SelectionStart and SelectionLength and restore the selection after the scroll. 如果要保留选择,则需要存储SelectionStartSelectionLength并在滚动后恢复选择。

你可能只是在前面加上你的消息,例如,最新的消息总是在顶部?

rchLog.Text = newMessage + "\n" + rchLog.Text;

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

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