C# 防止 RichTextBox 滚动/跳到顶部 [英] C# Preventing RichTextBox from scrolling/jumping to top

查看:80
本文介绍了C# 防止 RichTextBox 滚动/跳到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,当使用 System.Windows.Forms.RichTextBox 时,您可以使用 textbox.AppendText()textbox.Text = ""代码> 用于向文本框添加文本.

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 会滚​​动到底部,直接添加文本不会滚动,但是当用户聚焦文本框时会跳到顶部.

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.

这是我的功能:

// 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;
        }
    });
}

我还尝试导入 user32.dll 并覆盖滚动功能,但效果不佳.

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.

推荐答案

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

这并不像你认为的那样.它是一个赋值,它完全取代了 Text 属性.+= 运算符是方便的语法糖,但实际执行的代码是

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 不费力地将旧文本与新文本进行比较,以寻找可以将插入符号位置保持在同一位置的可能匹配项.因此,它会将插入符号移回文本中的第一行.这反过来又导致它向后滚动.跳跃.

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.

你肯定想避免这种代码,它非常昂贵.令人不快的是,如果您尝试格式化文本,则会丢失格式.相反,更喜欢 AppendText() 方法来附加文本和 SelectionText 属性来插入文本(在更改 SelectionStart 属性之后).不仅具有速度优势,而且无需滚动.

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.

这篇关于C# 防止 RichTextBox 滚动/跳到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆