在vb2008里设计一个每隔0.1秒就更新时间的秒表

可以用timer组件吗?为什么数字更改不了?谢谢
2024-11-24 16:18:32
推荐回答(2个)
回答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
End Sub给力不

回答2:

Public Class Form1
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Text = Format(Now, "yyyy-MM-dd HH:mm:ss:fff")
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 100
        Timer1.Start()
    End Sub
End Class