3

我正在使用下面的代码以hh:mm:ss格式显示剩余时间,例如,如果持续时间是30min,它将显示为这样00:30:00,1 分钟后将显示00:29:00,我怎样才能显示剩余的秒数并相应地减少它们。,

编辑

我试过timer1.Interval = 1000;
examTime = examTime.Subtract(TimeSpan.FromSeconds(1));

但是它没有显示我每秒减少的秒数,我该怎么做?

        public SubjectExamStart()
        {
            InitializeComponent();

             examTime = TimeSpan.FromMinutes(double.Parse(conf[1]));
                    label1.Text = examTime.ToString();
                    timer1.Interval = 60 * 1000;
                    timer1.Start();
         }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (sender == timer1)
            {
                if (examTime.TotalMinutes > 0)
                {
                    examTime = examTime.Subtract(TimeSpan.FromMinutes(1));
                    label1.Text = examTime.ToString();
                }
                else
                {
                    timer1.Stop();
                    MessageBox.Show("Exam Time is Finished");
                }
            }
        }
4

3 回答 3

3

而不是减去TimeSpan.FromMinutes你需要减去TimeSpan.FromSeconds

        public SubjectExamStart()
        {
            InitializeComponent();

             examTime = TimeSpan.FromSeconds(double.Parse(conf[1]));
                    label1.Text = examTime.ToString();
                    timer1.Interval = 1000;
                    timer1.Start();
         }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (sender == timer1)
            {
                if (examTime.TotalMinutes > 0)
                {
                    examTime = examTime.Subtract(TimeSpan.FromSeconds(1));
                    label1.Text = examTime.ToString();
                }
                else
                {
                    timer1.Stop();
                    MessageBox.Show("Exam Time is Finished");
                }
            }
        }

如果要在分配给标签时格式化时间跨度值...您可以在下面使用..

label1.Text = examTime.ToString(@"dd\.hh\:mm\:ss");
于 2013-10-30T11:39:05.457 回答
3

To do this properly, you will need to keep a track of when the timer was started

DateTime examStartTime;
System.Windows.Forms.Timer runTimer;
TimeSpan totalExamTime = new TimeSpan(1, 30, 0); // Set exam time to 1 hour 30 minutes.

if (runTimer == null)
    runTimer = new System.Windows.Forms.Timer();
runTimer.Interval = 200;
runTimer.Tick -= new EventHandler(runTimerTick);
runTimer.Tick += new EventHandler(runTimerTick);
examStartTime = DateTime.Now;
runTimer.Start();

Then in the event handler you can do:

public void runTimerTick(object sender, EventArgs e)
{
    TimeSpan currentExamTime = DateTime.Now - examStartTime;
    if (currentExamTime > totalExamTime)
    {
        MessageBox.Show("Exam Time is Finished");
        runTimer.Stop();
        runTimer.Tick -= new EventHandler(runTimerTick);
        runTimer.Dispose();
    }
}

I hope this helps.

于 2013-10-30T10:28:39.243 回答
0

试试这个希望这对你有用

设置定时器间隔=1000

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

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

                var seconds = timeSpan.Seconds;
                 if (seconds.ToString().Length.Equals(1))
                {
                    Sec = "0" + seconds.ToString();
                }
                else
                {
                    Sec = seconds.ToString();
                }

                string Totaltime = "Remaing Second: " + Sec;
                lblpurgingTimer.Text = Totaltime;
            }
于 2015-08-26T08:58:07.663 回答