0

它总是显示第一个条件。就像它点击了两次一样,我希望 a1 和 a2 可见,并且 label7 和 label8 以及很快......这是我的代码。任何帮助表示赞赏

Private Sub cmdLap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLap.Click

Dim a As Integer

a = a + 1

If a = 1 Then

   a1.Visible = True

   Label7.Text = Label1.Text & " hour(s) " & Label2.Text & " minute(s) " &   Label3.Text & " second(s) "

ElseIf a = 2 Then

   a2.Visible = True

   Label8.Text = Label1.Text & " hour(s) " & Label2.Text & " minute(s) " & Label3.Text & " second(s) "

ElseIf a = 3 Then

    a3.Visible = True

    Label9.Text = Label1.Text & " hour(s) " & Label2.Text & " minute(s) " & Label3.Text & " second(s) "

End If
End Sub**
4

2 回答 2

0

您的变量a是声明的 Sub 的本地变量。这意味着:每次调用此子(用户单击按钮)时,a都会在本地堆栈上创建一个新副本,并且其值再次从零开始。

您需要在 Sub 之外声明该变量。

Dim a As Integer

Private Sub cmdLap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLap.Click
  ......
End Sub     

当然现在你有相反的问题。该变量保持其先前的值,因此,在单击 4 次后,它达到值 4,并且不再与您的测试匹配。

By the way, do not use so undescriptive names for variables and controls. Try to use a more significative name to help your future coding efforts when six month from now, you will look back at this code.

于 2013-03-30T17:19:38.430 回答
0

a is local to the function so it's 0 each time you click, increments to 1, and you get that condition.

Move it to a class level property or field.

于 2013-03-30T17:20:00.200 回答