如何做30分钟倒计时 [英] How to do a 30 minute count down timer

查看:56
本文介绍了如何做30分钟倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 textbox1.Text 倒计时 30 分钟.到目前为止,我有这个:

I want my textbox1.Text to countdown for 30 minutes. So far I have this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Timer timeX = new Timer();
        timeX.Interval = 1800000;
        timeX.Tick += new EventHandler(timeX_Tick);
    }

    void timeX_Tick(object sender, EventArgs e)
    {
        // what do i put here?
    }
}

但是我现在很难过.我在 Google 上搜索了答案,但找不到与我的问题相符的答案.

However I'm now stumped. I checked Google for answers but couldn't find one matching my question.

推荐答案

如果您只想将 Texbox 的值设置为从 30 分钟开始倒计时.您首先需要将计时器间隔更改为小于 30 分钟.类似于 timeX.Interval = 1000; 的东西,它会每秒触发一次.然后像这样设置您的活动:

If all you want to do is set the value of your Texbox to count down from 30 Minutes. You will first need to change your timer interval to something smaller than 30Minutes. Something like timeX.Interval = 1000; which will fire every second. then set up your event like so:

 int OrigTime = 1800;
 void timeX_Tick(object sender, EventArgs e)
 {
     OrigTime--;
     textBox1.Text = OrigTime/60 + ":" + ((OrigTime % 60) >= 10 ?  (OrigTime % 60).ToString() : "0" + OrigTime % 60);
 }

同样在你的按钮点击中,你必须添加以下行:timeX.Enabled = true;为了启动计时器.

Also in your button click, you must add the following line: timeX.Enabled = true; In order to start the timer.

这篇关于如何做30分钟倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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