1

I'm Trying to make a small program using C# that tells me when my next metro is due for. I've coded this so far but when I change my system time, the Next Metro label doesn't change. Any Ideas what's going wrong?

namespace MetroAlarm 
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       public string time;
       public DateTime nextTime;

       private void Form1_Load(object sender, EventArgs e)
       {
          UpdateTime();
          UpdateNext();
       }

       public void UpdateTime()
       {
          time = DateTime.Now.ToString("h:mm:ss tt");
          LBLTime.Text = time;
          LBLNext.Text = nextTime;
       }

       public void UpdateNext()
       {
          if (DateTime.Now <= Metro1)
          {
             nextTime = Metro1;
          }

          if(DateTime.Now >= Metro1)
          {
             nextTime = Metro2;
          }

          if (DateTime.Now >= Metro2 )
          {
             nextTime = Metro3;
          } 

          if (DateTime.Now >= Metro3)
          {
             nextTime = Metro4;
          }

          if (DateTime.Now >= Metro4)
          {
             nextTime = Metro5;
          }

          if (DateTime.Now >= Metro5)
          {
             nextTime = LastMetro;
          }
      }

       private void UpdateClock_Tick(object sender, EventArgs e)
       {
          UpdateTime();
          UpdateNext();
       }

       DateTime Metro1 = Convert.ToDateTime("7:57:00 AM");
       DateTime Metro2 = Convert.ToDateTime("8:09:00 AM");
       DateTime Metro3 = Convert.ToDateTime("8:20:00 AM");
       DateTime Metro4 = Convert.ToDateTime("8:33:00 AM");
       DateTime LastMetro = Convert.ToDateTime("8:45:00 AM");
   }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • What's the LBLNext now? And whats your time now? – wingerse Sep 30 '15 at 20:19
  • yah you only call UpdateTime() when you first load. See this here - http://stackoverflow.com/questions/9725180/c-sharp-event-to-detect-daylight-saving-or-even-manual-time-change - set a handler for the SystemEvents timechanged – Darren Wainwright Sep 30 '15 at 20:19
  • I See you didn't accept any answer yet. if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution. – Salah Akbari Oct 01 '15 at 06:09
  • Didn't really have time to solve it but the answers in here didn't help me solve the problem. – user3654336 Oct 02 '15 at 22:17

1 Answers1

2

You need a Timer like this:

Timer timer1 = new Timer();

public Form1()
{
    InitializeComponent();
    timer1.Tick += UpdateClock_Tick;
    timer1.Start();
}

Also to show nextTime in the LBLNext.Text you should use ToShortTimeString() like this:

public void UpdateTime()
{
    time = DateTime.Now.ToString("h:mm:ss tt");
    LBLTime.Text = time;
    LBLNext.Text = nextTime.ToShortTimeString();
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109