VB.Net Timer 控件的用法

 为了研究VB.Net Timer 控件的用法,做了个计时器。

最后发现用途很广泛,例如说通过本软件精确的统计到今天晚上洗澡用去了21分52秒,类似的用途很有很多,本程序使用VS2005开发,需要.net FrameWork2.0支持。

代码如下:

------Form1.vb-----------------------------------------------------------------------------------------------------

Public Class Form1
    Private Shared myTimer As New System.Windows.Forms.Timer()
    Private Shared alarmCounter As Integer = 0'总秒数计数用
    Private Shared pbCount As Integer = 0'进度条计数用
    Private Shared timeH As Integer = 0'小时计数用
    Private Shared timeM As Integer = 0'分钟计数用
    Private Shared timeS As Integer = 0'秒计数用

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Main()
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 1000 '单位是毫秒
        Me.Timer1.Start()

        ' alarmCounter += 1
        ' myTimer.Enabled = True

    End Sub

'每经过1000毫秒就执行一次 Timer1_Tick方法
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        timeS += 1 '每一秒加一
        If timeS >= 60 Then
            timeS = 0
            timeM += 1
        End If

        If timeM >= 60 Then
            timeH += 1
            timeM = 0
            timeS = 0
        End If

        lblS.Text = timeS
        lblM.Text = timeM
        lblH.Text = timeH

        alarmCounter += 1
        pbCount += 1
        Me.lblSecand.Text = alarmCounter
        If Me.ProgressBar1.Value < 100 Then
            Me.ProgressBar1.Value = pbCount
        Else
            Me.ProgressBar1.Value = 0
            pbCount = 1
        End If
        Me.Text = "计时器: " & alarmCounter & "秒"
    End Sub

    Private Sub Class1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblS.Text = ""
        lblM.Text = ""
        lblH.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Timer1.Stop()'stop按钮
    End Sub

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

'Reset按钮事件
        Me.Timer1.Enabled = False
        lblS.Text = ""
        lblM.Text = ""
        lblH.Text = ""
        Me.lblSecand.Text = ""
        timeH = 0
        timeM = 0
        timeS = 0
        alarmCounter = 0
        Me.Text = "计时器 v1.0"
        Me.ProgressBar1.Value = 0
    End Sub
End Class

 

你可能感兴趣的:(VB.Net Timer 控件的用法)