单击按钮时如何制作自动倒数计时器? [英] How to make auto countdown timer when button is clicked?

查看:95
本文介绍了单击按钮时如何制作自动倒数计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!因此,我想在单击按钮时创建一个自动倒数计时器,如果可能,当计时器返回到0时,然后通知或MsgBox弹出并显示我写在其中的自定义消息。我不知道如何编程,所以如果你能帮助我,这可能真棒!谢谢。



我尝试过的事情:



我不知道知道如何编程所以帮助我



谢谢。

Hi! So I would like to make an auto countdown timer when button is clicked and also if possible when the timer goes back to 0, then a notification or MsgBox popup and show a custom message I write in it. I do not know how to program it so if you are able to help me this could be awesome! Thanks.

What I have tried:

Well I do not know how to program it so help me

Thanks.

推荐答案

假设Windows窗体:首先添加表格的计时器。

将间隔设置为1000(一秒)

添加一个类级别DateTime变量,并将其设置为您希望它显示消息的时间。

向Timer添加Tick事件处理程序,并在单击按钮时启动计时器。

在Tick处理程序方法中,读取时间:DateTime.Now将为您提供。

将其与您的存储值进行比较。

如果存储的值在当前时间之后,您可以根据需要通过减法显示差异:

Assuming Windows Forms: Start by adding a timer to your form.
Set the Interval to 1000 (one second)
Add a class level DateTime variable, and set it to the time you want it to show the message.
Add a Tick event handler to the Timer, and Start the timer on your button click.
In the Tick handler method, read the time: DateTime.Now will give you it.
Compare that with your stored value.
If the stored value is after the current time, you can show the difference via subtraction if you want:
myLablel.Text = (stopTime - currentTime).TotalSeconds.ToString()



如果不是,请停止计时器并显示您的消息。


If it isn't, stop the timer and display your message.


这是一个循序渐进的教程:教程2:创建一个定时数学测验 - Visual Studio | Microsoft Docs [ ^ ]
Here is a step-by-step tutorial: Tutorial 2: Create a timed math quiz - Visual Studio | Microsoft Docs[^]


这篇关于单击按钮时如何制作自动倒数计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆