Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

vb.net - 如何制作一个自定义控件的倒数计时器(How to make a countdown timer that a custom control)

I am trying to create a count down timer control that I will be adding to a bigger project later.

(我试图创建一个倒数计时器控件,稍后将其添加到一个更大的项目中。)

The control I am trying to make is a countdown timer that is given an initial value of 60 secs but also allows the user to change that value if needed.

(我想做的控件是一个倒数计时器,其初始值为60秒,但也允许用户在需要时更改该值。)

I am doing this in Visual Studio using Visual Basics.

(我正在使用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
  ask by Thomas translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Set your Timer Interval to something less than one second;

(将您的计时器间隔设置为小于一秒;)

I used 250.

(我用了250)

Then store the time in the future that is XXX seconds away, representing your countdown duration.

(然后将未来的时间存储在XXX秒之外,代表您的倒计时持续时间。)

At each tick, simply subtract the current time from the stored future time to get a TimeSpan.

(在每个刻度上,只需从存储的未来时间中减去当前时间即可获得TimeSpan。)

Update your label with the TimeSpan value using ToString().

(使用ToString()使用TimeSpan值更新标签。)

When the HowLong property is changed, update the target time and restart your timer...easy peesy.

(更改HowLong属性后,请更新目标时间并重新启动计时器...容易犯错误。)

All together, it'd look something like this:

(总之,看起来像这样:)

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

The authors response:

(作者回应:)

Technically your way will work to I will post my solution below I did it slightly differently.

(从技术上讲,您的方法将起作用,我将在稍稍不同的地方发布我的解决方案。)

– Thomas

(–托马斯)

From my comments on the authors own submission:

(根据我对作者本人提交的意见的评论:)

The problem with this type of approach is that the Timer control is not accurate.

(这种方法的问题是Timer控件不准确。)

It is only guaranteed to not fire before the interval has transpired.

(仅保证在间隔发生之前不会触发。)

In fact it will almost always fire after the interval with some extra "slop".

(实际上,在间隔之后,它几乎总是会发出一些额外的“倾斜”信号。)

For short periods (seconds/minutes), you won't notice.

(在短时间内(秒/分钟),您不会注意到。)

For longer periods (hours), you will, as the accumulated slop becomes bigger as time passes.

(对于更长的时间(小时),随着时间的流逝,累积的斜率会变大,您会这样做。)

Whether this matters is completely dependent upon your application.

(这是否重要完全取决于您的应用程序。)

– Idle_Mind

(– Idle_Mind)

Technically speaking, here's a quick example of how inaccurate simply incrementing/decrementing a counter using a 1 second Timer can be:

(从技术上讲,这是一个简单的示例,说明使用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

After only 1 hour and 15 minutes, the counter method on the left is already off by 4 seconds from the actual time that has passed:

(仅1小时15分钟之后,左侧的计数器方法已经比实际经过的时间缩短了4秒:)

在此处输入图片说明

A key advantage of the DateTime/TimeSpan method is that the time calculation is independent from the Timer.

(DateTime / TimeSpan方法的主要优点是时间计算独立于Timer。)

That is to say that the frequency at which the Timer fires has no bearing on how accurate the time calculation is.

(也就是说,计时器触发的频率与时间计算的准确性无关。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...