看来,使用System.Windows.Forms.RichTextBox
时,您可以使用textbox.AppendText()
或textbox.Text = ""
向文本框中添加文本。C#防止RichTextBox滚动/跳到顶部
AppendText
将滚动到底部并直接添加文本不会滚动,但会在用户关注文本框时跳转到顶部。
这里是我的功能:
// 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
和重载滚动功能,没有工作这么好。
有没有人知道如何一劳永逸地停止滚动文本框?
它不应该走到最前面,也不会走到最前面,当然也不会当前选择,而是保持目前的状态。
这实际上会滚动到底部,如果集中并且不重点。 – user1137183