简体   繁体   English

如何从文本框中更新标签文本

[英]how to make a labels text update from a textbox

I have a label with a maximum length of 15 characters, and a multi-line textbox with a max length of basically infinity. 我有一个最大长度为15个字符的标签,以及一个多行文本框,其最大长度基本上为无穷大。 I want it to when I type into the textbox to update its text to the label, BUT when the label reaches it's make length to remove the first character and replace the last character with the next letter in the textbox. 我希望它在输入文本框时将其文本更新为标签,但是当标签到达它的长度时,要删除文本框中的第一个字符并用下一个字母替换最后一个字符。 So basically it looks like a marquee left effect but updates in real time as I type. 因此,基本上,它看起来像是选取框左效果,但在我键入时会实时更新。 How would I do this? 我该怎么做?

This is what I have come up with 这就是我想出的

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

But it deletes the text after it reached 15 characters and writes again i want it to stream the text as if it were scrolling off to the left. 但是在达到15个字符后,它将删除该文本,然后再次写入,我希望它像流向左滚动一样流文本。

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

tho if you simply want to fix the code you've got replace this 如果您只是想修复代码,请替换此代码

int startIndex = ((text.Length - 1) / 15) * 15;

with this 有了这个

int startIndex = text.Length - 15;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM