How to do it:
You can use System.Windows.Forms.Timer
class to keep displaying your remaining time. You can do it in the following steps:
Create and initialize a timer:
Timer timer1 = new Timer();
Create its tick
event method and set interval to update the display time:
timer1.Tick += timer1_Tick;
timer1.Interval = 1000; //i am setting it for one second
Now start the timer:
timer1.Enabled = true;
timer1.Start();
Create timer.tick
event method and update the label at every second:
void timer1_Tick(object sender, EventArgs e)
{
TimeSpan TimeRemaining = VoteTime - DateTime.Now;
label1.Text = TimeRemaining.Hours + " : " + TimeRemaining.Minutes + " : " + TimeRemaining.Seconds;
}
Complete Code:
Here is the complete code. You can just copy and paste it:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Tick += timer1_Tick;
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
Timer timer1 = new Timer();
void timer1_Tick(object sender, EventArgs e)
{
TimeSpan TimeRemaining = VoteTime - DateTime.Now;
label1.Text = TimeRemaining.Hours + " : " + TimeRemaining.Minutes + " : " + TimeRemaining.Seconds;
}