2013-01-19 139 views
3

有什麼辦法可以壓縮這段代碼嗎?它的作用是在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"; 
     } 
    } 

回答

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

(注意,未經測試)

+1

正是我想要寫:) – Destrictor

+0

呀良好,它只是彈出說無法找到字符串:/ – Terrii

+0

@ Terrii抱歉,錯字是'Substring',已更正。 –

2
string all = "Hmm... You right there??"; 
if (textBox1.Text.Length < all.Length) 
    textBox1.Text += all[textBox1.Text.Length]; 
else 
    timer1.Stop(); 
+0

這只是發佈H一遍又一遍又沒有到m? – Terrii

+0

@Terrii:不,它會在TextBox中的最後一個字符後添加下一個字符(根據完整字符串)。 Ideone不喜歡Winforms,否則我會證明它。 –

+0

是的,我做到了,但它只是每100ms發送一個H – Terrii