Question

I have an application that, when a certain action occurs, the exact DATE/TIME is written as myTime using the Visual studion configuration manager where you can add settings.

This is my setting : Properties.Settings.Default.voteTime

I want as soon as my application starts to show a label that will display "X time left until next vote request"

In my context, the votes must be done each 12 hours so

I want the label to basically show how much time is left from those 12 hours, starting from the voteTime I mentionned above.

I have tried many techniques but I'm a noob at C# and noone worked for me, each time the label either had his default text or was blank...

        DateTime voteTime = Properties.Settings.Default.voteTime;
        DateTime startDate = DateTime.Now;

        //Calculate countdown timer.
        TimeSpan t = voteTime - startDate;
        string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", t.Days, t.Hours, t.Minutes, t.Seconds);

Above, that is what I tried then I wrote label1.text = countDown;

Thanks in advance.

Was it helpful?

Solution

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

OTHER TIPS

Here's a simple way, using a timer control, that will update the label every minute:

    TimeSpan TimeLeft = new TimeSpan();
    DateTime voteTime = Properties.Settings.Default.voteTime;      
    public Form3()
    {
        InitializeComponent();
        TimeLeft = voteTime - DateTime.Now;
        label1.Text = TimeLeft.ToString(@"hh\:mm") + " til launch.";
        //This value is in milliseconds.  Adjust this for a different time 
        //interval between updates
        timer1.Interval = 60000;
        timer1.Start();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        TimeLeft = voteTime - DateTime.Now;
        label1.Text = TimeLeft.ToString(@"hh\:mm") + " til launch.";
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top