2

我要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?
    }
}

但是我现在很难过。我检查了谷歌的答案,但找不到与我的问题相匹配的答案。

4

5 回答 5

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;
        }
    }
}
于 2013-05-18T03:21:18.607 回答
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";
        }

    }
}
于 2013-05-18T03:42:48.500 回答
2

如果您只想将 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;为了启动计时器。

于 2013-05-18T03:01:50.603 回答
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();
  }
于 2013-05-18T03:10:45.217 回答
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;
            }
         }
于 2015-08-26T09:03:24.040 回答