views:

919

answers:

1

I have a readonly data logging window that I implemented using the RichTextBox control. I'd like to be able to disable the autoscrolling that happens when the user clicks in the control so that the user can select a specific log for copy/paste operations or whatever. However, as soon as the user clicks in the RichTextBox, it automatically scrolls to the bottom, making this difficult.

Anyone know a way to override this behavior?

Thanks!

+1  A: 

You might take a look at doing something like this:

DllImport("user32.dll", EntryPoint = "LockWindowUpdate", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LockWindow(IntPtr Handle);

then in your method that appends log data (I'm making some assumptions here) you might do something like this:

LockWindow(this.Handle);
int pos = richTextBox1.SelectionStart;
int len = richTextBox1.SelectionLength;
richTextBox1.AppendText(yourText);
richTextBox1.SelectionStart = pos;
richTextBox1.SelectionLength = len;
LockWindow(IntPtr.Zero);

I did a little test app with a timer that did the append on the richtextbox and it stopped it from scrolling so I could do the text selection. It has some positional issues and isn't perfect, but perhaps it'll help move you toward a solution of your own.

All the best!

itsmatt