在VB里面,如果想让一个数字每隔0.1秒就-1,改怎么办

2024-11-07 13:43:51
推荐回答(4个)
回答(1):

您好,这样:
private DateTime TimeStart = new DateTime();
private Boolean IsFirst = true;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (IsFirst)
{
TimeStart = DateTime.Now;
timer1.Enabled = true;
IsFirst = false;
}
else
{
timer1.Enabled = !timer1.Enabled;
}
}
private void button3_Click(object sender, EventArgs e)
{
TimeStart = DateTime.Now;

这是Web制作秒表的代码 希望能帮助你
1)
启动visual basic6.0 ,打开一个新的标准工程。
2)
在窗体上Form1上添加6个标签空间(Label) 2个命令按钮(CommandButton)和1个计时器(Timer)。命令按钮的Caption属性分别为“启动”“停止”
Timer1的Interval属性为1
Label1 Label2 Label3的Caption属性分别为“开始时间”“结束时间”“经过时间” Timer1的Enable属性为False
3)代码
Dim Starttime As Variant
Dim End time As Variant
Dim Elapsed As Variant

Private Sub cmdStart_Click()
'显示开始时间
lblStart.Caption=Time$
Starttime=Now
'启动时钟控件
Timer1.Enabled=Ture
End Sub
Private Sub cmdStop_Click()
'记录停止时间
Endtime=Now
'关闭时钟控件
Timer1.Enabled=False
'显示经过时间
lblApaed.Caption=Format(Endtime-Starttime,"hh:mm:ss"
End Sub
Private Sub Timer1_Timer()
lblStop.Caption=Time$
End Sub
以上是用VB6.0实现的在窗体内加入timer1控件,然后复制下面代码,运行即可:
Option Explicit

Dim x As Long
Dim h As Long, m As Long, s As Long, ms As Long
Dim cjlh As Long, cjlm As Long, cjls As Long
Dim cjlms As String

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 13
Timer1.Enabled = True
Case 32
Timer1.Enabled = False
x = 0
Me.Caption = "运行了" & IIf(Len(Trim(Str(h))) < 2, "0" & Trim(Str(h)), Trim(Str(h))) & "小时" & IIf(Len(Trim(Str(m))) < 2, "0" & Trim(Str(m)), Trim(Str(m))) & "分" & IIf(Len(Trim(Str(s))) < 2, "0" & Trim(Str(s)), Trim(Str(s))) & "秒" & cjlms & "毫秒"
Case 114, 82
Me.Cls
End Select
End Sub

Private Sub Form_Load()
Me.Font.Size = 14
Me.AutoRedraw = True
Form1.Caption = "真正的动态秒表(小时:分:秒.毫秒)"
Print "00:00:00.000"
Timer1.Interval = 10
Timer1.Enabled = False
x = 0
End Sub

Private Sub Timer1_Timer() '每1秒钟触发100次
x = x + 10 '单位是毫秒
cjlh = Int(x / 3600000)
h = cjlh '取得小时
cjlm = Int(((x Mod 3600000) / 60000)) '关键在这里,用INT取整,不用INT的话,每30秒进1,有点奇怪
m = cjlm '取得分钟
cjls = Int(((x Mod 3600000) Mod 60000) / 1000)
s = cjls '取得秒种
ms = (((x Mod 3600000) Mod 60000) Mod 1000)
If Len(Trim(Str(ms))) = 2 Then
cjlms = "0" & Trim(Str(ms))
End If
If Len(Trim(Str(ms))) >= 3 Then
cjlms = Trim(Str(ms))
End If
cjlms = Left(cjlms, 2) & Trim(Str(Int(Rnd * 9)))
Me.Cls
Print IIf(Len(Trim(Str(h))) < 2, "0" & Trim(Str(h)), Trim(Str(h))) + ":" + IIf(Len(Trim(Str(m))) < 2, "0" & Trim(Str(m)), Trim(Str(m))) + ":" + IIf(Len(Trim(Str(s))) < 2, "0" & Trim(Str(s)), Trim(Str(s))) + "." + cjlms

回答(2):

用Timer控件,设置Timer1.Interval = 100

以下是演示代码:


Option Explicit
    Dim sj As Long
    
Private Sub Form_Load()
    sj = 100
    Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
    sj = sj - 1
    If sj <= 40 Then Timer1.Enabled = False
    Debug.Print sj; Time
End Sub

 不建议使用SleepAPI函数,它会使程序暂停若干时间,而无法干其它事。

回答(3):

自定义一个变量如i给i赋初值 用Timer控件,Timer属性里时间间隔设为0.1,在Timer时写入代码i=i-1,再用两个按钮控制Timer工作与停止就好了,我是小白不知道回的的对不。

回答(4):

用Timer控件不就行吗