您遇到的主要问题之一是 ProgressBar 需要整数,而 Timespan 的 Hour、Minute 和 Seconds 属性只给出整数,即 2 分钟将返回 2 而它将是 120 秒,因此您需要将它们乘以公共在这种情况下,我使用秒的分辨率。我创建了一个将 DateTimePicker 设置为 Time 的示例,一个间隔为 1 秒的 Timer,并创建了一个函数来区分两个 DateTime 对象。看看是否适合你。
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = GetTimeDifference(DateTimePicker1.Value, DateTime.Now)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim value As Integer = ProgressBar1.Maximum - GetTimeDifference(DateTimePicker1.Value, DateTime.Now)
If value > ProgressBar1.Maximum Then
Timer1.Stop()
Exit Sub
End If
ProgressBar1.Value = value
End Sub
Public Function GetTimeDifference(EndTime As DateTime, StartTime As DateTime) As Integer
Dim span As TimeSpan = EndTime.TimeOfDay - StartTime.TimeOfDay
Dim hour As Integer = span.Hours * 360
Dim minute As Integer = span.Minutes * 60
Dim second As Integer = span.Seconds
Dim result As Integer = hour + minute + second
Return result
End Function
End Class
根据康拉德评论修改功能:
Public Function GetTimeDifference(EndTime As DateTime, StartTime As DateTime) As Integer
Dim span As TimeSpan = EndTime.TimeOfDay - StartTime.TimeOfDay
Dim result As Integer = CInt(span.TotalSeconds)
Return result
End Function