1

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.

Dan
  • 57
  • 1
  • 4
  • 10
  • You should rephrase your post into a question rather than a specification. This is not a code-request site. – Blorgbeard Jul 07 '13 at 04:34

2 Answers2

7

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;
        }
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • Thank you, I was able to make my own out of this but there is onle little problem, I must make it detect days too. Because let's say you vote on the 7 july at 9PM, it would only show 3 hours... – Dan Jul 08 '13 at 03:14
  • 1
    @Dan15 Just change `TimeRemaining = VoteTime - DateTime.Now;` to `TimeRemaining = VoteTime.AddHours(12) - DateTime.Now;` – Shaharyar Jul 08 '13 at 16:14
3

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.";
    }
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • Thank you very much but, I have updated this code to match my application and the timer either shows 23:59 or 00:00 time left... I would like it for example to show 06:10 as an example. – Dan Jul 08 '13 at 03:39
  • 1
    Do you mean you want the target time to be 12 hours after the voteTime? If so use `voteTime.AddHours(12.0) - DateTime.Now` – tinstaafl Jul 08 '13 at 04:27
  • Yes exactly, after the person voted, start the countdown at 12hours then make it go down... to show how much time left – Dan Jul 08 '13 at 05:31