简体   繁体   中英

RichTextBox scroll

I'm not sure what is going on with a RichTextBox in a Visual Studio C# windows application. 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. (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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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