请帮我的计时器 [英] Help my timer please

查看:112
本文介绍了请帮我的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有疑问.我希望当我按下另一个CommandButton时计时器仍然运行.但是它停止了,只有一个运行.请帮帮我.

I have problem with the following code. I want the timer still run when I press the other CommandButton. But it stops and only one runs. Help me, please.

Option Explicit
Dim tora As Integer
Private Sub Command1_Click()
tora = 1
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
tora = 2
Timer1.Enabled = True
End Sub
Private Sub Command3_Click()
tora = 3
Timer1.Enabled = True
End Sub
Private Sub Command4_Click()
tora = 4
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Select Case tora
    Case 1
        Label1.Caption = Val(Label1.Caption) + 1
    Case 2
        Label2.Caption = Val(Label2.Caption) + 1
    Case 3
        Label3.Caption = Val(Label3.Caption) + 1
    Case 4
        Label4.Caption = Val(Label4.Caption) + 1
End Select
End Sub

推荐答案

我认为您的意思是我希望在计时器计时时更改多个标签."

如果是这样,那么您的操作方式将无法正常工作:当按下一个按钮时,它将覆盖其他按钮的操作.
而是设置四个bool变量,每个按钮一个,最初将它们设置为false.
在每个按钮例程中,反转这些变量之一的状态(如果为true,则将其设置为false.如果为false,则将其设置为true)
在计时器事件中,请分别检查每个变量,如果为true,则更新相应的标签.您不能为此使用Select Case:您需要四个If语句.
I think that what you mean is "I want multiple labels to change when the timer ticks."

If so, then the way you are doing it is not going to work: when a button is pressed, it overwrites what the other buttons have done.
Instead, set up four bool variables, one for each button, and initially make them false.
In each button routine, invert the status of one of these variables (if it is true, make it false. If it is false, make it true)
In your timer event, check each variable separately, and if it is true, update the appropriate label. You can''t use Select Case for this: you want four If statements.


您引用的是同一Timer类. "Timer1".对于每个Command_Click,您都启用相同的计时器.
只有1个计时器在运行,而不是4个.
You are referring to the same Timer class. "Timer1". For every Command_Click you enable the same timer.
Only 1 timer is running, not 4.


这篇关于请帮我的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆