2011-12-20 114 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

相关问题