Vb.Net我想在我的功能中修复一些东西 - 秒表

时间:2012-06-14 13:14:35

标签: vb.net stopwatch

我制作将时间字符串(“hh \:mm \:ss \,fff” - 例如:“00:00:00,100”)转换为零件的功能
strTime =“00:00:00,100”=
h int = 0
m int = 0
sec int = 0
millisec int = 100


功能:

Public Function ShowInLabel(ByVal TEXT As String, ByVal time As String, ByVal startTime As Boolean) As Boolean
        On Error Resume Next
        Dim sss As String
        sss = time
        Dim start As String = StrReverse(sss)
        start = StrReverse(start.Substring(0, 3))
        Dim s As Integer
        s = Integer.Parse(start)
        Dim secstart As String = StrReverse(sss).Substring(0, 6)
        secstart = StrReverse(secstart)
        Dim secs As Integer = Integer.Parse(secstart.Substring(0, 2))
        Dim hurs As Integer = Integer.Parse(sss.Substring(0, 2))
        Dim mins As Integer = Integer.Parse(StrReverse(StrReverse(sss.Substring(0, 5)).Substring(0, 2)))

        Dim stopWatch As New Stopwatch()
        stopWatch.Start()
noh:
        If stopWatch.Elapsed.Hours = hurs Then
            GoTo yesh
        Else
            GoTo noh
        End If
yesh:
        If stopWatch.Elapsed.Minutes = mins Then
            GoTo yesm
        Else
            GoTo yesh
        End If
yesm:
        If stopWatch.Elapsed.Seconds = secs Then
            GoTo yess
        Else
            GoTo yesm
        End If
yess:

        If stopWatch.Elapsed.Milliseconds > s Or stopWatch.Elapsed.Milliseconds = s Then
            GoTo done
        Else
            GoTo yess
        End If
done:
        If startTime = False Then
            Label1.Text = ""
        Else
            Label1.Text = TEXT
        End If

        Return True

    End Function



例:

ShowInLabel("SubTitle", "00:00:00,100", True)

功能有效,
但是当运行应用程序的函数被Stucked Till 时,函数返回true 为什么会这样?

1 个答案:

答案 0 :(得分:1)

你需要做的就是这样:

    Dim time As Date = DateTime.ParseExact("00:01:02,123", "hh:mm:ss,fff", CultureInfo.InvariantCulture)
    Dim h As Integer = time.Hour
    Dim m As Integer = time.Minute
    Dim sec As Integer = time.Second
    Dim millisec As Integer = time.Millisecond

然而,只要熟悉你想要完成的事情:),我怀疑你真正需要的是:

    Dim time As Date = DateTime.ParseExact("00:01:02,123", "hh:mm:ss,fff", CultureInfo.InvariantCulture)
    Dim startTime As Date = DateTime.ParseExact("00:00:00,000", "hh:mm:ss,fff", CultureInfo.InvariantCulture)
    Dim elapsed As TimeSpan = time - startTime
    Dim totalMilliseconds As Integer = CType(elapsed.TotalMilliseconds, Integer)

您可以以相同的方式将每个字幕的开始和结束时间转换为总毫秒,然后以这种方式进行比较。

正如其他人所指出的,On Error Resume Next仅在VB.NET中可用于向后兼容VB6代码。您应该使用Try / Catch块。然而,只要在整个方法上面放一个简历就不会被认为是好习惯,即使在VB6中也是如此,就像在整个方法中放置一个try / catch块一样也是一个坏主意。

同样地,GoTo只是程序员感受到的最糟糕的事情。您应该考虑其他选项,例如循环,if / else块,将代码分解为单独的方法等,并且不惜一切代价避免GoTo。