Prevent Autoscrolling in RichTextBox

后端 未结 3 2052
萌比男神i
萌比男神i 2020-12-03 08:46

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

3条回答
  •  失恋的感觉
    2020-12-03 09:17

    You might take a look at doing something like this:

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

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

    LockWindowUpdate(this.Handle);
    int pos = richTextBox1.SelectionStart;
    int len = richTextBox1.SelectionLength;
    richTextBox1.AppendText(yourText);
    richTextBox1.SelectionStart = pos;
    richTextBox1.SelectionLength = len;
    LockWindowUpdate(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!

提交回复
热议问题