有什么方法可以缩短这段代码吗?

时间:2011-12-20 22:10:42

标签: vb.net text progress-bar countdown

这是我想缩短的代码,它使用了一个进度条和一个倒计时的标签:我正在使用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

1 个答案:

答案 0 :(得分: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