[英]autoscroll a textbox in c# 1 line down every x seconds
我试图让文本框每X秒向下自动滚动一行。 我已经找到了AutoScrollOffset和ScrollToCaret函数,但是这些函数没有给出期望的结果。
我认为我的解决方案是在backgroundworkerthread中执行自动滚动功能,该功能每x秒向下滚动1行。 但是我不知道该怎么做,来自网络的信息也不是非常有用。
希望有人能帮助我,提前谢谢!
(即使用.net 4.5)
从此答案中大量借用并与Timer
结合使用,您可以执行以下操作:
private int lineNumber = 1;
private void timer1_Tick(object sender, EventArgs e)
{
nfobox.HideSelection = false;
nfobox.SelectionStart = nfobox.GetFirstCharIndexFromLine(lineNumber - 1);
nfobox.SelectionLength = nfobox.Lines[lineNumber - 1].Length;
nfobox.ScrollToCaret();
lineNumber++;
// include some code to detect the last line, or you'll get an exception
}
仅当您的行由换行符分隔时,此方法才有效。
如果是一条长线,那么整个内容将在第一个“刻度”上选中,然后完成。
“格兰特·温尼”是严厉的。 您不能从后台线程直接修改Uİ。 但是您使用下面的方法。
int lineCounter = 0;
int nextLineLength = 0;
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.SelectionStart = nextLineLength;
textBox1.SelectionLength = 0;
textBox1.ScrollToCaret();
nextLineLength += textBox1.Lines[lineCounter++].Length + "\r\n".Length; //"\r\n" is next line parameters
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.