| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2128 人关注过本帖
标题:关于exit sub,请指教
只看楼主 加入收藏
白掌七里香
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2018-9-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
关于exit sub,请指教
有一个红绿灯程序,红灯从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
搜索更多相关主题的帖子: exit sub Private End If 
2018-09-19 12:18
wds1
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:49
帖 子:393
专家分:2025
注 册:2016-3-10
收藏
得分:7 
问题原因:
  你在调用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编辑过]

2018-09-19 15:19
白掌七里香
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2018-9-19
收藏
得分:0 
回复 2楼 wds1
您好,用了您的方法,还是不行,我列出的程序是正确的,我的意思是去掉三个exit sub,怎么做?因为去掉exit sub,绿灯就从9开始计时了,黄灯就从1开始计时了
2018-09-19 15:33
wds1
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:49
帖 子:393
专家分:2025
注 册:2016-3-10
收藏
得分:0 
考虑了下,用停关定时器比较麻烦,把你的程序稍微简化了下,与你的程序跑的结果一致。



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

2018-09-19 16:33
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
收藏
得分:7 
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
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


请不要选我!!!
2018-09-19 21:05
zlf2000000
Rank: 1
等 级:新手上路
帖 子:1
专家分:7
注 册:2009-7-9
收藏
得分:7 
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
2018-09-25 12:19
快速回复:关于exit sub,请指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022247 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved