0

我制作了将时间字符串 ("hh\:mm\:ss\,fff" - 例如:"00:00:00,100") 转换为部分
strTime = "00:00:00,100" =
h int = 0
m int = 0
sec int = 0
毫秒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)

函数 Works ,
但是当运行应用程序的函数被卡住直到函数返回 true
为什么会发生这种情况?

4

1 回答 1

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。

于 2012-06-14T13:38:06.923 回答