views:

23

answers:

2

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?

A: 

You could just prepend your messages such that the newest message is always on the top?

rchLog.Text = newMessage + "\n" + rchLog.Text;
Steve Danner
+1  A: 
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.

Albin Sunnanbo
Thanks. This seems to work. I still don't know why my two programs differ in performance, though.
SeaDrive
@SeaDrive: You said nothing about performance in your question. What's about it?
Albin Sunnanbo
re: performance. I'm not sure why one version of the program scrolls and one doesn't. Nothing to do with performance in the sense of execution speed.
SeaDrive