vb怎么设计简单的计时器

用vb中的timer控件,设计计时器。用三个命令按钮,分别为:开始、暂停、结束。再用一个标签或者文本框显示就好了。各位大侠教教,我初学。... 用vb中的timer控件,设计计时器。用三个命令按钮,分别为:开始、暂停、结束。再用一个标签或者文本框显示就好了。
各位大侠教教,我初学。
展开
 我来答
_昕0昕_
2011-05-29 · TA获得超过574个赞
知道小有建树答主
回答量:158
采纳率:0%
帮助的人:190万
展开全部

控件:左侧序号(TextBox):p1 p2 p3 p4 p5 p6 p7 p8

右侧时间(TextBox):t t1 t2 t3 t4 t5 t6 t7 t8

下侧按钮(Button):Button1 Button2 Button3

'正文---------------------------------------------------------------

Public Class Form1

    Dim StartTime As Decimal

    Dim GettedTime As Decimal

    Dim TimeOfTop As Decimal

    Dim StopMoment As Byte

    Dim FirstTime As Decimal

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        t1.Text = ""

        t2.Text = ""

        t3.Text = ""

        t4.Text = ""

        t5.Text = ""

        t6.Text = ""

        t7.Text = ""

        t8.Text = ""

        a2.Text = ""

        a3.Text = ""

        a4.Text = ""

        a5.Text = ""

        a6.Text = ""

        a7.Text = ""

        a8.Text = ""

        t.Text = "0.0    "

        p1.Visible = False

        p2.Visible = False

        p3.Visible = False

        p4.Visible = False

        p5.Visible = False

        p6.Visible = False

        p7.Visible = False

        p8.Visible = False

        t1.Visible = False

        t2.Visible = False

        t3.Visible = False

        t4.Visible = False

        t5.Visible = False

        t6.Visible = False

        t7.Visible = False

        t8.Visible = False

        a2.Visible = False

        a3.Visible = False

        a4.Visible = False

        a5.Visible = False

        a6.Visible = False

        a7.Visible = False

        a8.Visible = False

        Button1.Enabled = True

        Button2.Enabled = True

        Timer1.Enabled = False

        Timer2.Enabled = False

        Timer3.Enabled = False

        Timer4.Enabled = False

        Timer5.Enabled = False

        Timer6.Enabled = False

        Timer7.Enabled = False

        Timer8.Enabled = False

        a2.Left = 71

        a3.Left = 71

        a4.Left = 71

        a5.Left = 71

        a6.Left = 71

        a7.Left = 71

        a8.Left = 71

    End Sub

    Private Function ToTime(ByVal TimeLength As Decimal) As String

        Dim h As Decimal

        Dim m As Decimal

        Dim s As Decimal

        Dim Hours As String

        Dim Minutes As String

        Dim Seconds As String

        Dim NoHour As Boolean

        Dim NoMinutes As Boolean

        If Int(TimeLength / 3600) = 0 Then

            Hours = ""

            NoHour = True

        Else

            NoHour = False

            h = Int(TimeLength / 3600)

            Hours = h & ":"

        End If

        m = Int((TimeLength - h * 3600) / 60)

        If m.ToString.Length = 1 And m <> 0 Then

            NoMinutes = False

            If NoHour = True Then

                Minutes = m & ":"

            Else

                Minutes = "0" & m & ":"

            End If

        ElseIf m = 0 Then

            NoMinutes = True

            If NoHour = True Then

                Minutes = ""

            Else

                Minutes = "0" & m & ":"

            End If

        Else

            NoMinutes = False

            Minutes = m & ":"

        End If

        s = TimeLength - 3600 * h - 60 * m

        If Int(s).ToString.Length = 2 Or (NoHour = True And NoMinutes = True) Then

            Seconds = s

        Else

            Seconds = "0" & s

        End If

        If NoHour = True And NoMinutes = True And Int(Seconds).ToString.Length = 1 Then

            Select Case Seconds.Length

                Case 1

                    Seconds = Seconds & ".000"

                Case 3

                    Seconds = Seconds & "00"

                Case 4

                    Seconds = Seconds & "0"

            End Select

        Else

            Select Case Seconds.Length

                Case 2

                    Seconds = Seconds & ".000"

                Case 4

                    Seconds = Seconds & "00"

                Case 5

                    Seconds = Seconds & "0"

            End Select

        End If

        ToTime = Hours & Minutes & Seconds

    End Function

    Private Sub GetTime()

        GettedTime = DateDiff(DateInterval.Second, Today, Now) + My.Computer.Clock.LocalTime.Millisecond / 1000

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        GetTime()

        StartTime = GettedTime

        Timer1.Enabled = True

        t.Text = "0.0    "

        Button1.Enabled = False

    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        GetTime()

        If TimeOfTop + 0.1 < GettedTime - StartTime Then

            TimeOfTop = TimeOfTop + 0.1

            t.Text = ToTime(TimeOfTop).Remove(ToTime(TimeOfTop).Length - 2) & "    "

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        StopMoment = StopMoment + 1

        Select Case StopMoment

            Case 1

                GetTime()

                t1.Text = ToTime(GettedTime - StartTime)

                FirstTime = GettedTime

                p1.Visible = True

                t1.Visible = True

            Case 2

                GetTime()

                t2.Text = ToTime(GettedTime - StartTime)

                a2.Text = "+" & ToTime(GettedTime - FirstTime)

                Timer2.Enabled = True

                p2.Visible = True

                t2.Visible = True

                a2.Visible = True

            Case 3

                GetTime()

                t3.Text = ToTime(GettedTime - StartTime)

                a3.Text = "+" & ToTime(GettedTime - FirstTime)

                Timer3.Enabled = True

                p3.Visible = True

                t3.Visible = True

                a3.Visible = True

            Case 4

                GetTime()

                t4.Text = ToTime(GettedTime - StartTime)

                a4.Text = "+" & ToTime(GettedTime - FirstTime)

                Timer4.Enabled = True

                p4.Visible = True

                t4.Visible = True

                a4.Visible = True

            Case 5

                GetTime()

                t5.Text = ToTime(GettedTime - StartTime)

                a5.Text = "+" & ToTime(GettedTime - FirstTime)

                Timer5.Enabled = True

                p5.Visible = True

                t5.Visible = True

                a5.Visible = True

            Case 6

                GetTime()

                t6.Text = ToTime(GettedTime - StartTime)

                a6.Text = "+" & ToTime(GettedTime - FirstTime)

                Timer6.Enabled = True

                p6.Visible = True

                t6.Visible = True

                a6.Visible = True

            Case 7

                GetTime()

                t7.Text = ToTime(GettedTime - StartTime)

                a7.Text = "+" & ToTime(GettedTime - FirstTime)

                Timer7.Enabled = True

                p7.Visible = True

                t7.Visible = True

                a7.Visible = True

            Case 8

                GetTime()

                t8.Text = ToTime(GettedTime - StartTime)

                a8.Text = "+" & ToTime(GettedTime - FirstTime)

                Timer8.Enabled = True

                p8.Visible = True

                t8.Visible = True

                a8.Visible = True

                Timer1.Enabled = False

                t.Text = t8.Text

                Button2.Enabled = False

        End Select

    End Sub

    Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

        StartTime = 0

        GettedTime = 0

        TimeOfTop = 0

        StopMoment = 0

        FirstTime = 0

        t1.Text = ""

        t2.Text = ""

        t3.Text = ""

        t4.Text = ""

        t5.Text = ""

        t6.Text = ""

        t7.Text = ""

        t8.Text = ""

        a2.Text = ""

        a3.Text = ""

        a4.Text = ""

        a5.Text = ""

        a6.Text = ""

        a7.Text = ""

        a8.Text = ""

        t.Text = "0.0    "

        p1.Visible = False

        p2.Visible = False

        p3.Visible = False

        p4.Visible = False

        p5.Visible = False

        p6.Visible = False

        p7.Visible = False

        p8.Visible = False

        t1.Visible = False

        t2.Visible = False

        t3.Visible = False

        t4.Visible = False

        t5.Visible = False

        t6.Visible = False

        t7.Visible = False

        t8.Visible = False

        a2.Visible = False

        a3.Visible = False

        a4.Visible = False

        a5.Visible = False

        a6.Visible = False

        a7.Visible = False

        a8.Visible = False

        Button1.Enabled = True

        Button2.Enabled = True

        Timer1.Enabled = False

        Timer2.Enabled = False

        Timer3.Enabled = False

        Timer4.Enabled = False

        Timer5.Enabled = False

        Timer6.Enabled = False

        Timer7.Enabled = False

        Timer8.Enabled = False

        a2.Left = 71

        a3.Left = 71

        a4.Left = 71

        a5.Left = 71

        a6.Left = 71

        a7.Left = 71

        a8.Left = 71

    End Sub

    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        a2.Left = a2.Left + 10

        If a2.Left = 171 Then

            Timer2.Enabled = False

        End If

    End Sub

    Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer3.Tick

        a3.Left = a3.Left + 10

        If a3.Left = 171 Then

            Timer3.Enabled = False

        End If

    End Sub

    Private Sub Timer4_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer4.Tick

        a4.Left = a4.Left + 10

        If a4.Left = 171 Then

            Timer4.Enabled = False

        End If

    End Sub

    Private Sub Timer5_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer5.Tick

        a5.Left = a5.Left + 10

        If a5.Left = 171 Then

            Timer5.Enabled = False

        End If

    End Sub

    Private Sub Timer6_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer6.Tick

        a6.Left = a6.Left + 10

        If a6.Left = 171 Then

            Timer6.Enabled = False

        End If

    End Sub

    Private Sub Timer7_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer7.Tick

        a7.Left = a7.Left + 10

        If a7.Left = 171 Then

            Timer7.Enabled = False

        End If

    End Sub

    Private Sub Timer8_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer8.Tick

        a8.Left = a8.Left + 10

        If a8.Left = 171 Then

            Timer8.Enabled = False

        End If

    End Sub

End Class

'正文完-------------------------------------------------------------

博思aippt
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT... 点击进入详情页
本回答由博思aippt提供
mijing2000
2011-05-24 · TA获得超过282个赞
知道小有建树答主
回答量:372
采纳率:0%
帮助的人:305万
展开全部
Private Sub Command1_Click()
Timer1.Enabled = True

End Sub

Private Sub Command2_Click()
Timer1.Enabled = False

End Sub

Private Sub Command3_Click()
Timer1.Enabled = False
Label1.Caption = 0
End Sub

Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
s = 0
Label1.Caption = s
End Sub

Private Sub Timer1_Timer()
s = Label1.Caption
s = s + 1
Label1.Caption = s
End Sub
追问
请问可以在精确吗?
设置成00:00:00的那种
追答
这是我在另一个帖子的回复,我看看差不多
Private Sub Command1_Click()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

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

Private Sub Timer1_Timer()
Static a As Long
Static b As Long
Static c As Long
a = a + 1
If a > 59 Then
a = 0
b = b + 1
ElseIf b > 59 Then
b = 0
c = c + 1
End If
Text1.Text = "已经过了" & c & "小时" & b & "分" & a & "秒"
End Sub
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
damyzl
2011-05-31 · TA获得超过1586个赞
知道大有可为答主
回答量:1475
采纳率:74%
帮助的人:881万
展开全部
Dim nCount As Long

Private Sub cmdPause_Click()
Timer1.Enabled = False

End Sub

Private Sub cmdStart_Click()
Timer1.Enabled = True

End Sub

Private Sub cmdStop_Click()
Timer1.Enabled = False
nCount = 0
Label1.Caption = Format(nCount)

End Sub

Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
nCount = 0
Label1.Caption = Format(nCount)
End Sub

Private Sub Timer1_Timer()
nCount = nCount + 1
Label1.Caption = Format(nCount)
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式