c# - 减少代码行数

标签 c# winforms textbox

有什么办法可以压缩这段代码吗?它所做的是在 100 毫秒后,计时器开始计时并发布下一个字母。这个有更小的代码版本吗?

我只想让它一次输入一个字符。

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

(注意,未经测试)

关于c# - 减少代码行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14419863/

相关文章:

c# - Silverlight 编程绘图(从 Windows 窗体转换为 Silverlight)

c# - 如何创建具有异步功能的类(类似于 SqlCommand 或 WebRequest)?

C# 是否可以将 groupBox 标题设为单选按钮?

c# - 为什么我的 WPF TextBox 不显示任何字符?

c++ - 如何在编辑控件上自动隐藏滚动条

javascript - 通过客户端脚本更改时访问服务器端只读文本框值的解决方法

c# - ASP.NET 没有向 SQL 服务器发送命令

C# 找不到类型或命名空间名称

c# - 我在哪里可以找到异步队列的当前内容?

C# 防止 AppDomain 程序集的类实例进行文件访问