0

我正在通过.net 构建一个闹钟。我想通过百分比(进度条)显示警报即将触发的时间。我目前有一个功能可以让我从警报时间和返回小时、分钟和秒的当前系统时间倒计时,但我没有能够弄清楚这一点..

Dim span As System.TimeSpan = TimeEnd.TimeOfDay - DateTime.Now.TimeOfDay
Dim remaining As String, remain As String, aTime As String

remaining = span.Hours & "hr:" & span.Minutes & "min:" & span.Seconds & "sec"
remain = span.Hours & span.Minutes & span.Seconds

For y = 0 To remain

    pBar.Minimum = 0
    pBar.Maximum = remain
    pBar.Value = y

    '  MsgBox("Min: " & pBar.Minimum & " Max:" & pBar.Maximum)
    Exit Sub


Next y

我想我需要澄清一下...

我有两次

闹钟时间:5:00:下午

当前时间:不管它是什么

我需要计算两个值之间的时间量,并在进度条中表示这两个值何时相遇,因为进度条会在它们接近时填满。

4

2 回答 2

1

您遇到的主要问题之一是 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
于 2013-01-26T07:32:10.083 回答
0
    Dim span As System.TimeSpan = TimeEnd.TimeOfDay - DateTime.Now.TimeOfDay
    Dim remaining As String, remain As String, aTime As String

    remaining = span.Hours & "hr:" & span.Minutes & "min:" & span.Seconds & "sec"
    remain = span.Hours & span.Minutes & span.Seconds

    For y = 0 To remain

        pBar.Minimum = 0
        pBar.Maximum = remain
        pBar.Value = y

        '  MsgBox("Min: " & pBar.Minimum & " Max:" & pBar.Maximum)
        Exit Sub


    Next y

我正在尝试将 pbar.maximum 设置为剩余时间,并随着时间接近警报时间而使其进展。

于 2013-01-26T05:55:20.537 回答