use*_*183 3 c# scroll richtextbox
似乎在使用时,System.Windows.Forms.RichTextBox您可以使用textbox.AppendText()或textbox.Text = ""在文本框中添加文本。
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;
}
});
}
Run Code Online (Sandbox Code Playgroud)
我还尝试了导入user32.dll和覆盖效果不佳的滚动功能。
有谁知道如何一劳永逸地停止滚动文本框?
它不应该到达顶部,也不应该到达底部,当然也不应该到达当前选择,而应该停留在当前位置。
console.Text += "\r\n" + line;
Run Code Online (Sandbox Code Playgroud)
那并没有您所想的那样。它是一个赋值,它完全替代了Text属性。+ =运算符是方便的语法糖,但是执行的实际代码是
console.Text = console.Text + "\r\n" + line;
Run Code Online (Sandbox Code Playgroud)
RichTextBox会毫不费力地将旧文本与新文本进行比较,以查找可能的匹配,以使插入符号位置保持在同一位置。因此,它将插入符号移回到文本的第一行。进而导致其向后滚动。跳。
您肯定要避免这种代码,因为它非常昂贵。而且,如果您不愿意对文本进行格式设置,那么您将失去格式设置。相反,赞成使用AppendText()方法来追加文本,而使用SelectionText属性来插入文本(更改SelectionStart属性之后)。不仅具有速度优势,而且也无滚动优势。
| 归档时间: |
|
| 查看次数: |
3195 次 |
| 最近记录: |