我正在尝试创建一个倒数计时器控件,稍后将其添加到更大的项目中。我正在尝试做的控件是一个倒数计时器,其初始值为60秒,但也允许用户在需要时更改该值。我正在使用Visual Basics在Visual Studio中进行此操作。
Public Class UserControl1
Dim timeTick As Integer
Dim min As Integer
Dim setSecs As Integer = 60
Dim sec As Integer = 120
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer.Start()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
sec -= 1
min = sec % 60
Label1.Text = min & " : " & sec
If sec < 60 Then
min = 1 + timeTick
Label1.Text = min & " : " & sec
End If
End Sub
Property HowLong As Integer
Get
Return setSecs
End Get
Set(value As Integer)
setSecs = value
End Set
End Property
End Class
答案 0 :(得分:1)
将您的计时器间隔设置为小于一秒;我用了250。
然后存储未来XXX秒后的时间,代表您的倒计时持续时间。
在每个刻度上,只需从存储的将来时间中减去当前时间即可获得TimeSpan。使用ToString()使用TimeSpan值更新标签。
更改HowLong属性后,请更新目标时间并重新启动计时器...容易犯错误。
总的来说,看起来像这样:
Public Class UserControl1
Private target As DateTime
Private setSecs As Integer = 60
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
target = DateTime.Now.AddSeconds(HowLong)
Timer.Start()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
Dim ts As TimeSpan = target.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
Label1.Text = "-" & ts.ToString("mm\:ss")
Else
Label1.Text = "00:00"
Timer.Stop()
End If
End Sub
Property HowLong As Integer
Get
Return setSecs
End Get
Set(value As Integer)
setSecs = value
Timer.Stop()
target = DateTime.Now.AddSeconds(HowLong)
Timer.Start()
End Set
End Property
End Class
作者回应:
从技术上讲,您的方法将起作用,我将在下面的步骤中发布我的解决方案 略有不同。 –托马斯
从我对作者自己提交的内容的评论来看:
这种方法的问题是Timer控件是 不准确。仅保证在间隔前不点火 发生了。实际上,间隔之后它几乎总是会触发 还有一些额外的“倾斜”。在短时间内(秒/分钟),您不会 注意。对于较长的时间(小时),您将作为累积的斜率 随着时间的流逝变得越来越大。这是否重要完全 取决于您的应用程序。 – Idle_Mind
从技术上讲,这是一个简单的示例,说明使用1秒计时器简单地递增/递减计数器可能不准确:
' Timer1.Interval was set to 1000 (timer fires every "second")
Private seconds As Integer = 0
Private start As DateTime = DateTime.Now
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
seconds = seconds + 1
Label1.Text = seconds
Label2.Text = DateTime.Now.Subtract(start).TotalSeconds
End Sub
仅1小时15分钟之后,左侧的计数器方法已经比实际经过的时间缩短了4秒:
DateTime / TimeSpan方法的一个主要优点是时间计算与计时器无关。也就是说,计时器触发的频率与时间计算的准确性无关。
答案 1 :(得分:0)
下面的代码是我如何制作一个简单的计时器控件,该控件从设置值开始倒计时。另外,它还有2个按钮可以暂停和恢复时间。如果在设计模式下且计时器间隔设置为1000,则此控件将不起作用。如果您对它的工作方式有任何疑问,请发表评论。
Public Class UserControl1
Dim timeRemaing As Integer = 60
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer.Start()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
Dim sec As Integer
Dim mins As Integer
Dim timerFormat As String
If Not Me.DesignMode Then
sec = timeRemaing Mod 60
mins = timeRemaing \ 60
If sec < 10 Then
timerFormat = mins.ToString + ":0" + sec.ToString
Else
timerFormat = mins.ToString + ":" + sec.ToString
End If
If timeRemaing > 0 Then
timeRemaing -= 1
lblTime.Text = timerFormat
Else
Timer.Stop()
lblTime.Text = "The Time has Stop!!!"
End If
End If
End Sub
Public Property HowLong As Integer
Get
Return timeRemaing
End Get
Set(value As Integer)
If value <= 0 Then
Timer.Stop()
ElseIf value > 0 Then
Timer.Start()
timeRemaing = value
End If
End Set
End Property
Private Sub btnPause_Click(sender As Object, e As EventArgs) Handles btnPause.Click
Timer.Stop()
End Sub
Private Sub btnResume_Click(sender As Object, e As EventArgs) Handles btnResume.Click
Timer.Start()
End Sub
End Class