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 whatev开发者_StackOverflow社区er.
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.
You could just prepend your messages such that the newest message is always on the top?
rchLog.Text = newMessage + "\n" + rchLog.Text;
精彩评论