C# Preventing RichTextBox from scrolling/jumping to top

不问归期 提交于 2020-01-02 07:04:56

问题


It appears that when using a System.Windows.Forms.RichTextBox you can use textbox.AppendText() or textbox.Text = "" for adding text to the textbox.

AppendText will scroll to the bottom and adding the text directly will not scroll, but will jump to the top when the user has the textbox focused.

Here is my function:

// Function to add a line to the textbox that gets called each time I want to add something
// console = textbox
public void addLine(String line)
{
    // Invoking since this function gets accessed by another thread
    console.Invoke((MethodInvoker)delegate
    {
        // Check if user wants the textbox to scroll
        if (Settings.Default.enableScrolling)
        {
            // Only normal inserting into textbox here with AppendText()
        }
        else
        {
            // This is the part that doesn't work
            // When adding text directly like this the textbox will jump to the top if the textbox is focused, which is pretty annoying
            Console.WriteLine(line);
            console.Text += "\r\n" + line;
        }
    });
}

I've also tried importing user32.dll and overriding the scroll functions which didn't work so well.

Does anybody know how to, once and for all, stop the scrolling of the textbox?

It shouldn't go to the top, neither to the bottom and of course also not to the current selection, but rather just stay where it is at the moment.


回答1:


 console.Text += "\r\n" + line;

That does not do what you think it does. it is an assignment, it completely replaces the Text property. The += operator is convenient syntax sugar but the actual code that executes is

 console.Text = console.Text + "\r\n" + line;

RichTextBox makes no effort to compare the old text with the new text to look for a possible match that could keep the caret position in the same place. It thus moves the caret back to the first line in the text. Which in turn causes it to scroll back. Jump.

You definitely want to avoid this kind of code, it is very expensive. And unpleasant if you made any effort to format the text, you'll lose the formatting. Instead favor the AppendText() method to append text and the SelectionText property to insert text (after changing the SelectionStart property). With the benefit of not just speed but no scrolling either.




回答2:


After this:

 Console.WriteLine(line);
 console.Text += "\r\n" + line;

just add this two lines:

console.Select(console.Text.Length-1, 1);
console.ScrollToCaret();

Happy coding




回答3:


Then, if I got you correctly, you should try this:

Console.WriteLine(line);
console.SelectionProtected = true;
console.Text += "\r\n" + line;

When I try it, it works like you want it to.




回答4:


I had to achieve something similar, so I wanted to share...

When:

  • Focused by user: no scroll
  • Not focused by user: scroll to bottom

I took Hans Passant's advice about using AppendText() and the SelectionStart property. Here is how my code looks like:

int caretPosition = myTextBox.SelectionStart;

myTextBox.AppendText("The text being appended \r\n");

if (myTextBox.Focused)
{
    myTextBox.Select(caretPosition, 0);
    myTextBox.ScrollToCaret();
}


来源:https://stackoverflow.com/questions/12787066/c-sharp-preventing-richtextbox-from-scrolling-jumping-to-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!