Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net visualbasic中的定时器_Vb.net_Timer - Fatal编程技术网

Vb.net visualbasic中的定时器

Vb.net visualbasic中的定时器,vb.net,timer,Vb.net,Timer,目前,我正在尝试在VisualBasic中创建一个需要start、resume/pause和stop方法的计时器。但是,当我添加停止方法时,计时器确实停止了,但没有重置计时器的值。有时,当我再次尝试启动它时,计时器会滴答作响到一个负值(例如:-15:-44:-12)。你能帮帮我吗?任何帮助和建议都会大有帮助 这是我最近一直在工作的代码 Public Class Form1 Dim StartTime As DateTime 'old current time Dim PauseT

目前,我正在尝试在VisualBasic中创建一个需要start、resume/pause和stop方法的计时器。但是,当我添加停止方法时,计时器确实停止了,但没有重置计时器的值。有时,当我再次尝试启动它时,计时器会滴答作响到一个负值(例如:-15:-44:-12)。你能帮帮我吗?任何帮助和建议都会大有帮助

这是我最近一直在工作的代码

Public Class Form1
    Dim StartTime As DateTime 'old current time
    Dim PauseTime As DateTime 'keep track of the time when the timer is paused
    Dim TotalTimePaused As TimeSpan 'The total amount of time paused

    Private Sub BtnStart1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart1.Click
        BtnStart1.Enabled = False
        BtnPause1.Enabled = True
        StartTime = DateTime.Now()
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Subtract the current "Now" time from the start time
        'and then subtract the total time paused
        Dim ElapsedTime As TimeSpan = DateAndTime.Now.Subtract(StartTime).Subtract(TotalTimePaused)
        Label2.Text = ElapsedTime.Hours.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime.Minutes.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime.Seconds.ToString.PadLeft(2, "0"c)
        'And if you wanted the total elapsed time from time of start then
        'subtract the current "Now" time from the start time only.
    End Sub
    Private Sub BtnPause1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPause1.Click
        If Timer1.Enabled = False Then
            BtnPause1.Text = "Pause"
            'Add to the total time paused; the current time minus the 
            'time of pause.  this will be the total amount of time
            'paused so you can subtract it from the total amount of
            'time since the start button was pressed
            TotalTimePaused = TotalTimePaused.Add(DateAndTime.Now.Subtract(PauseTime))
            Timer1.Enabled = True
        Else
            BtnPause1.Text = "Resume"
            Timer1.Enabled = False
            'Set the pause time so it can be
            'calculated to the total time paused.
            'when the timer is resumed
            PauseTime = DateAndTime.Now
        End If
    End Sub

    Private Sub BtnStop1_Click(sender As Object, e As EventArgs) Handles BtnStop1.Click
        If Timer1.Enabled = False Then
            BtnStart1.Enabled = True
            BtnPause1.Text = "Pause"
            TotalTimePaused = TotalTimePaused.Add(DateAndTime.Now.Subtract(PauseTime))
        Else
            Timer1.Enabled = True
        End If
    End Sub
End Class
你有这个功能

Private Sub BtnStop1_Click(sender As Object, e As EventArgs) Handles BtnStop1.Click
    If Timer1.Enabled = False Then
        BtnStart1.Enabled = True
        BtnPause1.Text = "Pause"
        TotalTimePaused = TotalTimePaused.Add(DateAndTime.Now.Subtract(PauseTime))
    Else
        Timer1.Enabled = True
    End If
End Sub
如果你停止计时,我想你应该改变这个

Private Sub BtnStop1_Click(sender As Object, e As EventArgs) Handles BtnStop1.Click
    If Timer1.Enabled = True Then
       Timer1.Enabled= False 'Here you stop the timer and 
                             'when you clic the start button this starts on
                             'on your time.
        BtnStart1.Enabled = True
        BtnPause1.Text = "Pause"
        TotalTimePaused = TotalTimePaused.Add(DateAndTime.Now.Subtract(PauseTime))
    Else
        Timer1.Enabled = False

    End If
End Sub

您必须在计时器运行时停止计时器。

是否不允许您使用
秒表类?