VB.net实时时间过去的功能

时间:2013-09-17 05:31:27

标签: vb.net timer countdowntimer

我已经创建了一个与计算机时间同步的实时时钟,并以标签显示。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub

我想制作一个实时的经过时间的功能,它一直计算从开始到停止的时间所经过的秒/分钟/小时,它将基于我创建的实时时钟。我会为此创建一个开始和停止按钮。这可能吗?提前致谢。


我现在能够完成所有内容,并且我添加了一个功能,根据我的实时时钟记录开始和结束时间。这是我的工作代码:

Dim hr, min, sec As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub

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

    Start.Text = ""
    EndLbl.Text = ""
    Elapse.Text = ""
    Timer2.Enabled = True
    Start.Text = Time.Text
End Sub

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

    sec = sec + 1
    If (sec = 60) Then
        sec = 0
        min = min + 1
    ElseIf (min = 60) Then
        min = 0
        hr = hr + 1
    ElseIf (hr = 24) Then
        hr = 0
        min = 0
        sec = 0
    End If

    Elapse.Text = String.Format("{0}hr : {1}min : {2}sec", hr, min, sec)
    Timer2.Interval = 1000
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Timer2.Enabled = False
    EndLbl.Text = Label4.Text

    hr = 0
    min = 0
    sec = 0
    Timer2.Interval = 1
End Sub

对NeverHopeless给出的起始代码的信用。非常感谢。

3 个答案:

答案 0 :(得分:7)

我建议你只使用1个计时器:

Public Class Form2

Private _elapseTimerRunning As Boolean = False
Private _elapseStartTime As DateTime

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
    Timer1.Interval = 1000
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    txtTime.Text = Now.ToString("h:mm:ss tt")
    If _elapseTimerRunning = True Then
        Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
        txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
    End If
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    _elapseStartTime = DateTime.Now
    _elapseTimerRunning = True
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    _elapseTimerRunning = False
End Sub

结束班

答案 1 :(得分:2)

显示应用程序运行所用时间的示例。

Public Class Form1
    'shows elapsed time that the app has run
    '
    Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
    Dim stpw As Stopwatch = Stopwatch.StartNew

    Private Sub tick(state As Object)
        If stpw.IsRunning Then
            'format - http://msdn.microsoft.com/en-us/library/ee372287.aspx
            Me.Invoke(Sub()
                          Label1.Text = stpw.Elapsed.ToString("d\ \ hh\:mm\:ss\.ff")
                      End Sub)
        End If
    End Sub

End Class

使用按钮添加启动/停止功能:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'start the elapsed timer
    stpw.Start() 'continue 
    'or
    'stpw.Restart() 'restart
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    stpw.Stop()
End Sub

答案 2 :(得分:1)

这样的事情会对你有所帮助:(未经测试,但会给你一个启动者)

Dim hr, min, sec As Integer 'msec;

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
    Timer2.Enabled = True
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'msec++;
    'if(msec == 60)  { msec = 0; sec++; }

    sec+=1;
    if(sec  = 60) Then 
      sec = 0 : min+=1
    end if
    if(min  = 60) Then
      min = 0 : hr+=1
    end if
    if(hr = 24)  Then
      hr  = 0 : min = 0 : sec = 0
    end if   

    'TimeElapsed.Text = String.Format("{0}:{1}:{2} {3}", hr, min, sec, msec)
     TimeElapsed.Text = String.Format("{0}:{1}:{2}", hr, min, sec) 
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub

注意: Timer2将针对每个second运行。