我有一个最大长度为15个字符的标签,以及一个最大长度为无穷大的多行文本框。当我在文本框中输入以更新标签的文本时,我希望它是这样的,但是当标签达到它的make length时,我需要删除第一个字符,并用文本框中的下一个字母替换最后一个字符。所以基本上它看起来像是字幕左边的效果,但随着我的输入,它会实时更新。我该怎么做呢?
这就是我想出来的
private void textBox1_TextChanged(object sender, EventArgs e)
{
String text = textBox1.Text.Replace("\r\n", "|");
int startIndex = ((text.Length - 1) / 15) * 15;
label1.Text = text.Substring(Math.Max(0, startIndex));
}
但它会在文本达到15个字符后删除文本,并重新写入。我想让它流式传输文本,就像向左滚动一样。
发布于 2012-02-14 06:02:13
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = textBox1.Text.Length <= 15
? textBox1.Text
: new string(textBox1.Text.Skip(textBox1.Text.Length - 15).ToArray());
}
如果您只是想修复已有的代码,请替换以下代码
int startIndex = ((text.Length - 1) / 15) * 15;
有了这个
int startIndex = text.Length - 15;
https://stackoverflow.com/questions/9272225
复制相似问题