How to do a 30 minute count down timer

2019-07-19 13:48发布

问题:

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

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

回答1:

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);
 }

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



回答2:

Here's a simple example similar to the code you posted:

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;
        }
    }
}


回答3:

Easiest thing you can do, is use a 1 minute timer:

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";
        }

    }
}


回答4:

Your code will only get one event fired, once the 30 minutes has passed. In order to keep updating your UI continuously you'll have to make the events more frequent and add a condition inside the event handler to tell the count-down to stop once 30 minutes has passed.

You can do the time calculations easily by using TimeSpan and DateTime.

You'll also want to make sure your UI code runs on the UI thread, hence the 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();
  }


回答5:

try this hope this will work for u

set timer interval=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;
            }
         }


标签: c# timer