如何做一个30分钟的倒计时器

时间:2013-05-18 02:55:05

标签: c# timer

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

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的答案,但找不到符合我问题的答案。

5 个答案:

答案 0 :(得分:11)

这是一个类似于您发布的代码的简单示例:

using System;
using System.Windows.Forms;

namespace StackOverflowCountDown
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            textBox1.Text = TimeSpan.FromMinutes(30).ToString();
        }

        private void Form1_Load(object sender, EventArgs e) { }

        private void textBox1_TextChanged(object sender, EventArgs e) { }

        private void button1_Click(object sender, EventArgs e)
        {
            var startTime = DateTime.Now;

            var timer = new Timer() { Interval = 1000 };

            timer.Tick += (obj, args) =>    
                textBox1.Text =
                    (TimeSpan.FromMinutes(30) - (DateTime.Now - startTime))
                    .ToString("hh\\:mm\\:ss");

            timer.Enabled = true;
        }
    }
}

答案 1 :(得分:3)

你能做的最简单的事情就是使用1分钟计时器:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace countdowntimer
{
    public partial class Form1 : Form
    {
        private Timer timeX;
        private int minutesLeft;

        public Form1()
        {
            InitializeComponent();

            timeX = new Timer(){Interval = 60000};
            timeX.Tick += new EventHandler(timeX_Tick);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {



        }

        private void button1_Click(object sender, EventArgs e)
        {
            minutesLeft=30;
            timeX.Start();
        }

        void timeX_Tick(object sender, EventArgs e)
        {

            if(minutesLeft--<=0)
            {
              timeX.Stop();
              // Done!
            }
            else
            {
              // Not done yet...
            }
            textBox1.Text = minutesLeft + " mins remaining";
        }

    }
}

答案 2 :(得分:1)

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

 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;以启动计时器。

答案 3 :(得分:1)

一旦30分钟过去,您的代码只会触发一个事件。为了不断更新您的UI,您必须更频繁地进行事件,并在事件处理程序中添加一个条件,以告知倒计时在30分钟过后停止。

您可以使用TimeSpan和DateTime轻松完成时间计算。

您还需要确保您的UI代码在UI线程上运行,因此Invoke

  timeX.Interval = 500;

...


  TimeSpan timeSpan = TimeSpan.FromMinutes(30);
  DataTime startedAt = DateTime.Now;
  void timeX_Tick(object sender, EventArgs e)
  { 
       if ((DateTime.Now - startedAt)<timeSpan){
          Invoke(()=>{
             TimeSpan remaining = timeSpan - (DateTime.Now - startedAt);
             textBox.Text = remaining.ToString(); 
          });
       } else
          timeX.Stop();
  }

答案 4 :(得分:0)

试试这个希望这对你来说是

  

设置计时器间隔= 1000

minremain=1800000; //Should be in milisecond
timerplurg.satrt();

 private void timerplurg_Tick(object sender, EventArgs e)
        {
       minremain = minremain - 1000;
        string Sec = string.Empty;
        string Min = string.Empty;
        if (minremain <= 0)
        {
            lblpurgingTimer.Text = "";
            timerplurg.Stop();
            return;
        }
        else
        {
            var timeSpan = TimeSpan.FromMilliseconds(Convert.ToDouble(minremain));

            var seconds = timeSpan.Seconds;
            var minutes = timeSpan.Minutes;
            if (seconds.ToString().Length.Equals(1))
            {
                Sec = "0" + seconds.ToString();
            }
            else
            {
                Sec = seconds.ToString();
            }
            if (minutes.ToString().Length.Equals(1))
            {
                Min = "0" + minutes.ToString();
            }
            else
            {
                Min = minutes.ToString();
            }
            string Totaltime = "Purge Remaing Time: " + Min + ":" + Sec;
            lblpurgingTimer.Text = Totaltime;
            }
         }