2011-12-20 112 views
2

這是我想shortenning的代碼,它使用一個進度條和標籤倒計時吧:我用VB語言有什麼辦法可以縮短這段代碼嗎?

If ProgressBar1.Value = 33 And Label2.Text = "Now: Hold down power button (3 seconds)" Then 
      Label2.Text = "Now: Hold down power button (2 seconds)" 
     End If 

     If ProgressBar1.Value = 66 And Label2.Text = "Now: Hold down power button (2 seconds)" Then 
      Label2.Text = "Now: Hold down power button (1 seconds)" 
     End If 

     If ProgressBar1.Value = 99 And Label2.Text = "Now: Hold down power button (1 seconds)" Then 
      Label2.Text = "Now: Hold down power button (0 seconds)" 
     End If 

     If ProgressBar1.Value = 100 And Label2.Text = "Now: Hold down power button (0 seconds)" Then 
      Label2.Text = "Now: Also hold down the home button (10 seconds)" 
      Label2.Location = New Point(30, Label2.Location.Y) 
      Label3.Text = "Next: Release the power button only (15 seconds)" 
      ProgressBar1.Value = 0 
     End If 

回答

3

是的,有:

Const BASE_MESSAGE As String = "Now: Hold down power button ({0} seconds)" 

    Select Case ProgressBar1.Value 
     Case 33 
      Label2.Text = String.Format(BASE_MESSAGE, 2) 
     Case 66 
      Label2.Text = String.Format(BASE_MESSAGE, 1) 
     Case 99 
      Label2.Text = String.Format(BASE_MESSAGE, 0) 

     Case 100 
      Label2.Text = "Now: Also hold down the home button (10 seconds)" 
      Label2.Location = New Point(30, Label2.Location.Y) 
      Label3.Text = "Next: Release the power button only (15 seconds)" 
      ProgressBar1.Value = 0 

    End Select 
+0

謝謝,我會在明天測試並回復給您:D – user1081679 2011-12-20 22:22:46

+0

完美地與具有兩個'結束'命令的終點線完全分開。感謝您的幫助:) – user1081679 2011-12-20 23:28:32

+0

@ user1081679:對不起,我已經修復了答案。另外,歡迎來到Stackoverflow!請記住,當答案解決或幫助您解決問題時,您應該點擊問題旁邊的複選標記和向上箭頭,讓未來的問題訪問者知道這是解決問題的方法。 – 2011-12-20 23:30:45