我试图使用一个按钮运行3个计时器,每次点击1个计时器都会运行。它在第二个计时器运行顺利,但在第3次点击第3个计时器没有运行。任何人都可以帮我这个吗?
这是我的代码
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Timer1.Enabled = False Then
Timer1.Start()
ElseIf Timer1.Enabled = True Then
Timer2.Start()
ElseIf Timer1.Enabled = True And
Timer2.Enabled = true Then
Timer3.Start()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Label1.Text + 1
If Label1.Text = 60 Then
Label1.Text = 0
Label2.Text += 1
ElseIf Label2.Text = 60 Then
Label2.Text = 0
Label3.Text += 1
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label6.Text = Label6.Text + 1
If Label6.Text = 60 Then
Label6.Text = 0
Label5.Text += 1
ElseIf Label5.Text = 60 Then
Label5.Text = 0
Label4.Text += 1
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Label12.Text = Label12.Text + 1
If Label12.Text = 60 Then
Label12.Text = 0
Label11.Text += 1
ElseIf Label11.Text = 60 Then
Label11.Text = 0
Label10.Text += 1
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
答案 0 :(得分:2)
代码中的问题在范围内,如果在(IF,ELSE IF,ELSE IF,ELSE)语句中找到一个条件,则代码将在该范围内输入,并且不会检查其他条件。例如,
IF (CONDITION)
..some code
ELSE IF(CONDITION) // if this condition is found true, then compiler will not check other condtion
..some code // this code will be executed only
ELSE IF(CONDITION)
..some code
ELSE
..some code
首先确保所有功能都启用为true或false,并按如下所示更改代码:
If Timer1.Enabled = False And Timer2.Enabled = False And Timer3.Enabled=False Then
Timer1.Start()
End If
If Timer1.Enabled = True And Timer2.Enabled = False And Timer3.Enabled=False Then
Timer2.Start()
End If
If Timer1.Enabled = True And Timer2.Enabled=True And Timer3.Enabled=False Then
Timer3.Start()
End If
答案 1 :(得分:1)
您需要了解If和ElseIf之间的区别
如果第一个条件匹配,它将不会进入下一个检查条件,因此在这种情况下,此代码必须像这样替换
If Timer1.Enabled = False Then
Timer1.Start()
End If
If Timer1.Enabled = True Then
Timer2.Start()
End If
If Timer1.Enabled = True And Timer2.Enabled = true Then
Timer3.Start()
End If
修改
根据你的评论你可以做这样的事情
Public Class Form1
Dim ClickCount as Integer
ClickCount = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ClickCount = ClickCount + 1
If Timer1.Enabled = False and ClickCount = 1 Then
Timer1.Start()
ElseIf Timer1.Enabled = True and ClickCount = 2 Then
Timer2.Start()
ElseIf Timer1.Enabled = True And Timer2.Enabled = true and ClickCount = 3 Then
Timer3.Start()
ClickCount = 0;
End If
End Sub
答案 2 :(得分:1)
您的代码永远不会进入第二个ElseIf
。从第三次单击按钮开始,语句Timer1.Enabled = True
仍为真,它将进入该部分代码。这使得该部分能够使第三个计时器无法访问。
你真正想要的是什么:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If Timer1.Enabled = True And Timer2.Enabled = True Then
Timer3.Start()
ElseIf Timer1.Enabled = True Then
Timer2.Start()
Else
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Label1.Text + 1
If Label1.Text = 60 Then
Label1.Text = 0
Label2.Text += 1
ElseIf Label2.Text = 60 Then
Label2.Text = 0
Label3.Text += 1
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer2.Tick
Label6.Text = Label6.Text + 1
If Label6.Text = 60 Then
Label6.Text = 0
Label5.Text += 1
ElseIf Label5.Text = 60 Then
Label5.Text = 0
Label4.Text += 1
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer3.Tick
Label12.Text = Label12.Text + 1
If Label12.Text = 60 Then
Label12.Text = 0
Label11.Text += 1
ElseIf Label11.Text = 60 Then
Label11.Text = 0
Label10.Text += 1
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
答案 3 :(得分:1)
你的第二个ElseIf的条件和第三个ElseIf的第一部分是相同的,这意味着第三个从不执行其中的代码,因为它总是落入第二个条件。
这将使您的代码更容易保存变量以跟踪您需要启动的计时器。此外,如果您使用案例陈述,那么添加更多的定时器是一项简单的任务。
这样的事情:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'declare a static variable which keeps its value
Static whichTimer As Integer = 0
'every time the button is clicked increment the value
whichTimer += 1
'now start the appropriate timer
Select Case whichTimer
Case 1
Timer1.Start()
Case 2
Timer2.Start()
Case 3
Timer3.Start()
Case Else
'don't care
End Select
End Sub