注册 登录
编程论坛 VB6论坛

关于exit sub,请指教

白掌七里香 发布于 2018-09-19 12:18, 2128 次点击
有一个红绿灯程序,红灯从5开始倒计时,绿灯从10开始倒计时,黄灯从2开始倒计时,依次顺序为红灯-绿灯-黄灯-红灯
程序加载时显示为红灯。想请教:在timer1_timer()中,如果不加exit sub,倒计时绿灯为什么从9开始,黄灯为什么从1开始
程序如下:
只有本站会员才能查看附件,请 登录

Dim x%
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False
End Sub

Private Sub Form_Load()
Call Option1_Click
End Sub

Private Sub Option1_Click()
Shape1.Visible = True
Shape2.Visible = False
Shape3.Visible = False
x = 5
Label1.Caption = x
Label1.ForeColor = vbRed
End Sub

Private Sub Option2_Click()
Shape1.Visible = False
Shape2.Visible = True
Shape3.Visible = False
 x = 2
 Label1.Caption = x
 Label1.ForeColor = vbYellow
End Sub

Private Sub Option3_Click()
Shape1.Visible = False
Shape2.Visible = False
Shape3.Visible = True
x = 10
Label1.Caption = x
Label1.ForeColor = vbGreen
End Sub

Private Sub Timer1_Timer()
If Option1.Value Then
   x = x - 1
If x < 0 Then Option3.Value = True: Exit Sub
End If

If Option3.Value Then
   x = x - 1
If x < 0 Then Option2.Value = True: Exit Sub
End If

If Option2.Value Then
   x = x - 1
If x < 0 Then Option1.Value = True: Exit Sub
End If
Label1.Caption = x
End Sub
5 回复
#2
wds12018-09-19 15:19
问题原因:
  你在调用option时,定时器并没有终止,在执行Option_Click时,又触发了定时器。
   
解决方法(更新,避免调用option响应后X值改变后在显示问题):
  Private Sub Timer1_Timer()
    x = x - 1
    Label1.Caption = x
    If Option1.Value Then
      If x < 0 Then Option3.Value = True
    End If
    If Option3.Value Then
     If x < 0 Then Option2.Value = True
    End If
    If Option2.Value Then
     If x < 0 Then Option1.Value = True
    End If
   End Sub
 


[此贴子已经被作者于2018-9-19 16:30编辑过]

#3
白掌七里香2018-09-19 15:33
回复 2楼 wds1
您好,用了您的方法,还是不行,我列出的程序是正确的,我的意思是去掉三个exit sub,怎么做?因为去掉exit sub,绿灯就从9开始计时了,黄灯就从1开始计时了
#4
wds12018-09-19 16:33
考虑了下,用停关定时器比较麻烦,把你的程序稍微简化了下,与你的程序跑的结果一致。



[此贴子已经被作者于2018-9-19 16:35编辑过]

#5
ZHRXJR2018-09-19 21:05
只有本站会员才能查看附件,请 登录

程序代码:

Dim x%
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False
End Sub

Private Sub Form_Load()
Call Option1_Click
Timer1.Enabled = False
Timer1.Interval = 1000
End Sub

Private Sub Option1_Click()
Shape1.FillColor = vbRed
Shape2.FillColor = RGB(127, 127, 127)
Shape3.FillColor = RGB(127, 127, 127)
x = 5
Label1.Caption = x
Label1.ForeColor = vbRed
End Sub

Private Sub Option2_Click()
Shape1.FillColor = RGB(127, 127, 127)
Shape2.FillColor = vbYellow
Shape3.FillColor = RGB(127, 127, 127)

 x = 3

 Label1.Caption = x

 Label1.ForeColor = vbYellow
End Sub

Private Sub Option3_Click()
Shape1.FillColor = RGB(127, 127, 127)
Shape2.FillColor = RGB(127, 127, 127)
Shape3.FillColor = vbGreen
x = 11
Label1.Caption = x
Label1.ForeColor = vbGreen
End Sub

Private Sub Timer1_Timer()
If Option1.Value Then
   x = x - 1
If x < 0 Then Option3.Value = True
End If

If Option3.Value Then
   x = x - 1
If x < 0 Then Option2.Value = True
End If

If Option2.Value Then
   x = x - 1
If x < 0 Then Option1.Value = True
End If
Label1.Caption = x
End Sub

#6
zlf20000002018-09-25 12:19
Dim lngTime As Long
Dim Oindex As Integer

Private Sub Command1_Click()
    Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
    Timer1.Enabled = False
End Sub

Private Sub Form_Load()
    lngTime = 0
    Oindex = 0
End Sub

Private Sub Option1_Click(Index As Integer)
    Dim i As Long
    For i = 0 To 2
        Shape1(i).Visible = False
    Next
    lngTime = VBA.IIf(Index = 0, 5, VBA.IIf(Index = 1, 2, 10))
    Option1(Index).Value = True
    Shape1(Index).Visible = True
End Sub

Private Sub Timer1_Timer()
    If lngTime < 0 Then
        Oindex = VBA.IIf(Oindex = 2, 0, Oindex + 1)
        Option1_Click Oindex
    Else
        Label1.Caption = lngTime
        lngTime = lngTime - 1
    End If
End Sub
1