c# – 减少代码行数

有没有办法压缩这段代码?这样做是在100ms的时间后计时器滴答并且它发布下一个字母.是否有较小的代码版本?

我只是想让它一次输出一个字符.

private void timer1_Tick(object sender, EventArgs e)
    {
        if (textBox2.Text == "Hmm... You right there?")
        {
            textBox2.Text = "Hmm... You right there??";
            timer1.Stop();
        }
        if (textBox2.Text == "Hmm... You right there")
        {
            textBox2.Text = "Hmm... You right there?";
        }
        if (textBox2.Text == "Hmm... You right ther")
        {
            textBox2.Text = "Hmm... You right there";
        }
        if (textBox2.Text == "Hmm... You right the")
        {
            textBox2.Text = "Hmm... You right ther";
        }
        if (textBox2.Text == "Hmm... You right th")
        {
            textBox2.Text = "Hmm... You right the";
        }
        if (textBox2.Text == "Hmm... You right t")
        {
            textBox2.Text = "Hmm... You right th";
        }
        if (textBox2.Text == "Hmm... You right ")
        {
            textBox2.Text = "Hmm... You right t";
        }
        if (textBox2.Text == "Hmm... You right")
        {
            textBox2.Text = "Hmm... You right ";
        }
        if (textBox2.Text == "Hmm... You righ")
        {
            textBox2.Text = "Hmm... You right";
        }
        if (textBox2.Text == "Hmm... You rig")
        {
            textBox2.Text = "Hmm... You righ";
        }
        if (textBox2.Text == "Hmm... You ri")
        {
            textBox2.Text = "Hmm... You rig";
        }
        if (textBox2.Text == "Hmm... You r")
        {
            textBox2.Text = "Hmm... You ri";
        }
        if (textBox2.Text == "Hmm... You ")
        {
            textBox2.Text = "Hmm... You r";
        }
        if (textBox2.Text == "Hmm... You")
        {
            textBox2.Text = "Hmm... You ";
        }
        if (textBox2.Text == "Hmm... Yo")
        {
            textBox2.Text = "Hmm... You";
        }
        if (textBox2.Text == "Hmm... Y")
        {
            textBox2.Text = "Hmm... Yo";
        }
        if (textBox2.Text == "Hmm... ")
        {
            textBox2.Text = "Hmm... Y";
        }
        if (textBox2.Text == "Hmm...")
        {
            textBox2.Text = "Hmm... ";
        }
        if (textBox2.Text == "Hmm..")
        {
            textBox2.Text = "Hmm...";
        }
        if (textBox2.Text == "Hmm.")
        {
            textBox2.Text = "Hmm..";
        }
        if (textBox2.Text == "Hmm")
        {
            textBox2.Text = "Hmm.";
        }
        if (textBox2.Text == "Hm")
        {
            textBox2.Text = "Hmm";
        }
        if (textBox2.Text == "H")
        {
            textBox2.Text = "Hm";
        }
        if (textBox2.Text == "")
        {
            textBox2.Text = "H";
        }
    }

解决方法:

string fullText = "Hmm... You right there??";
int currentPos = 0;

private void timer1_Tick(object sender, EventArgs e)
{
    currentPos++;
    textBox2.Text = fullText.Substring(0, currentPos);

    if (currentPos == fullText.Length)
        timer1.Stop();
}

(注意,未经测试)

上一篇:layui多选框


下一篇:Week-2-作业1