如何显示小时:分钟:秒:使用VB.NET在秒表中的毫秒? [英] How to show Hour:Minute:Second:Milisecond in Stopwatch using VB.NET?

查看:423
本文介绍了如何显示小时:分钟:秒:使用VB.NET在秒表中的毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Class Form1
    Dim running As Boolean = False
    Dim miliSeconds As Integer = 0, seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        running = True
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        running = False
        Timer1.Stop()
        Timer1.Enabled = False
    End Sub

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

        miliSeconds += 1
        If miliSeconds = 1000 Then
            miliSeconds = 0
            seconds += 1
            If (seconds = 60) Then
                seconds = 0
                minutes += 1
                If (minutes = 60) Then
                    minutes = 0
                    hours += 1
                End If
            End If
        End If
        Label1.Text = hours & ":" & minutes & ":" & seconds & ":" & miliSeconds
    End Sub
End Class







我已经使用了上面的代码,但它没有给出正确的毫秒级。如何获得正确的毫秒?




I have used the above code but it's not giving the right milisecond. How to get the right milisecond?

推荐答案

不要这样做。

相反,保持一个类级别DateTime值你设置为DateTime.Now启动计时器,并在Tick上生成已用时间:

Don't do it like that.
Instead, keep a class level DateTime value which you set to DateTime.Now when you start the timer, and generate the elapsed time on the Tick:
Dim diff As TimeSpan = DateTime.Now - startTime
Label1.Text = diff.ToString("hh\:mm\:ss\:fff")



由于Tick事件发生在在计时器到期之后的某个时刻(但不能保证完全正确),这是保持经过时间显示准确的唯一方法。



代码更改为VB [/ edit]


Since Tick events happen at some point after (but not guaranteed to be exactly at) the moment when the timer expires, it's the only way to keep the elapsed time display accurate.

[edit]Code changed to VB[/edit]


Public Class Form1
    Dim running As Boolean = False
    Dim miliSeconds As Integer = 0, seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0
    Dim startTime As DateTime
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

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

        startTime = DateTime.Now
        running = True
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        running = False
        Timer1.Stop()
        Timer1.Enabled = False
    End Sub

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

        'miliSeconds += 1
        'If miliSeconds = 1000 Then
        '    miliSeconds = 0
        '    seconds += 1
        '    If (seconds = 60) Then
        '        seconds = 0
        '        minutes += 1
        '        If (minutes = 60) Then
        '            minutes = 0
        '            hours += 1
        '        End If
        '    End If
        'End If
        'Label1.Text = hours & ":" & minutes & ":" & seconds & ":" & miliSeconds
        Dim diff As TimeSpan = DateTime.Now - startTime
        Label1.Text = diff.ToString("hh\:mm\:ss\:fff")

    End Sub


End Class







这个工作正常。谢谢你..




this is working fine. thank u..


这篇关于如何显示小时:分钟:秒:使用VB.NET在秒表中的毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆