K
K
Kirill Shashkov2018-09-19 17:41:52
C++ / C#
Kirill Shashkov, 2018-09-19 17:41:52

How can I change the color of the text at specified intervals?

Good day! I ask for help in understanding my mistakes -
there is a goal to create a clock that changes color (Red or Green) depending on the current period of time (according to a given schedule).
It is desirable without reference to a date, that is, after starting the program, it can function for more than 24 hours, non-stop, so to speak.
Below is my code. I can’t reach the right decision myself, I understand that I need to constantly check the current time with the schedule, but it turns out that only the last condition is taken into account and the color change does not occur further. Help to understand and understand the algorithm of actions.
Thank you in advance!

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace My_clock
{
  /// <summary>
  /// Description of MainForm.
  /// </summary>
  public partial class MainForm : Form
  {
        Timer timer = new Timer();
  
    public MainForm()
    {
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      TopMost = true;
      InitializeComponent();
      
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
    }
    void Timer1Tick(object sender, EventArgs e)
      
    {
      
  int h = DateTime.Now.Hour;
  int m = DateTime.Now.Minute;
  int s = DateTime.Now.Second;
 
  string time = "";
   if (h < 10)
 {
     time += "0" + h;
 }
 else
 {
     time += h;
 }
 
 time += ":";
 
 if (m < 10)
 {
     time += "0" + m;
 }
 else
 {
     time += m;
 }
 
 time += ":";
 
 if (s < 10)
 {
     time += "0" + s;
 }
 else
 {
     time += s;
 }
 
 			label1.Text = time;
 
 			var timeNow = DateTime.Now;
 			
 link1:	
 if (timeNow > DateTime.Parse("08:15:00") && timeNow < DateTime.Parse("09:00:00"));
    {
      label1.ForeColor = Color.Green;
    }
 			var period1 = DateTime.Parse("09:00:00");
 //if (timeNow > period1) goto link2;
 link2:
 if (timeNow > DateTime.Parse("09:00:00") && timeNow < DateTime.Parse("09:05:00"));
    {
      label1.ForeColor = Color.Red;
    }
  			var period2 = DateTime.Parse("09:05:00");
 //if (timeNow > period2) goto link3;
 link3:
 if (timeNow > DateTime.Parse("09:05:00") && timeNow < DateTime.Parse("09:50:00"));
    {
      label1.ForeColor = Color.Green;
    }
  			var period3 = DateTime.Parse("09:50:00");
 //if (timeNow > period3) goto link4;
 link4:
 if (timeNow > DateTime.Parse("09:50:00") && timeNow < DateTime.Parse("10:00:00"));
    {
      label1.ForeColor = Color.Red;
    }
  			var period4 = DateTime.Parse("10:00:00");
 //if (timeNow > period4) goto link5;
 link5:
 if (timeNow > DateTime.Parse("10:00:00") && timeNow < DateTime.Parse("10:45:00"));
    {
      label1.ForeColor = Color.Green;
    }
  			var period5 = DateTime.Parse("10:45:00");
 //if (timeNow > period5) goto link6;
 link6:
 if (timeNow > DateTime.Parse("10:45:00") && timeNow < DateTime.Parse("10:50:00"));
    {
      label1.ForeColor = Color.Red;
    }
  			var period6 = DateTime.Parse("10:50:00");
 //if (timeNow > period6) goto link7;
 link7:
 if (timeNow > DateTime.Parse("10:50:00") && timeNow < DateTime.Parse("11:35:00"));
    {
      label1.ForeColor = Color.Green;
    }
  			var period7 = DateTime.Parse("11:35:00");
 //if (timeNow > period7) goto link8;
 link8:
 if (timeNow > DateTime.Parse("11:35:00") && timeNow < DateTime.Parse("11:45:00"));
    {
      label1.ForeColor = Color.Red;
    }
  			var period8 = DateTime.Parse("11:45:00");
 //if (timeNow > period8) goto link9;
 link9:
 if (timeNow > DateTime.Parse("11:45:00") && timeNow < DateTime.Parse("12:30:00"));
    {
      label1.ForeColor = Color.Green;
    }
  			var period9 = DateTime.Parse("12:30:00");
 //if (timeNow > period9) goto link10;
 link10:
 if (timeNow > DateTime.Parse("12:30:00") && timeNow < DateTime.Parse("12:35:00"));
    {
      label1.ForeColor = Color.Red;
    }
  			var period10 = DateTime.Parse("12:35:00");
 //if (timeNow > period10) goto link11;
 link11:
 if (timeNow > DateTime.Parse("12:35:00") && timeNow < DateTime.Parse("13:20:00"));
    {
      label1.ForeColor = Color.Green;
    }
  			var period11 = DateTime.Parse("13:20:00");
 //if (timeNow > period11) goto link12;
 link12:
 if (timeNow > DateTime.Parse("13:20:00") && timeNow < DateTime.Parse("13:25:00"));
    {
      label1.ForeColor = Color.Red;
    }
  			var period12 = DateTime.Parse("13:25:00");
 //if (timeNow > period12) goto link13;
 link13:
 if (timeNow > DateTime.Parse("13:25:00") && timeNow < DateTime.Parse("14:10:00"));
    {
      label1.ForeColor = Color.Green;
    }
  			var period13 = DateTime.Parse("14:10:00");
 //if (timeNow > period13) goto link14;
 link14:
 if (timeNow > DateTime.Parse("14:10:00") && timeNow < DateTime.Parse("14:15:00"));
    {
      label1.ForeColor = Color.Red;
    }
  			var period14 = DateTime.Parse("14:15:00");
 //if (timeNow > period14) goto link15;
 link15:
 if (timeNow > DateTime.Parse("14:15:00") && timeNow < DateTime.Parse("15:00:00"));
    {
      label1.ForeColor = Color.Green;
    }
  			var period15 = DateTime.Parse("15:00:00");
 //if (timeNow > period15) goto link16;
 link16:
 if (timeNow > DateTime.Parse("15:00:00") && timeNow < DateTime.Parse("15:05:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period16 = DateTime.Parse("15:05:00");
 //if (timeNow > period16) goto link17;
 link17:
 if (timeNow > DateTime.Parse("15:05:00") && timeNow < DateTime.Parse("15:50:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period17 = DateTime.Parse("15:50:00");
 //if (timeNow > period17) goto link18;
 link18:
 if (timeNow > DateTime.Parse("15:50:00") && timeNow < DateTime.Parse("15:55:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period18 = DateTime.Parse("15:55:00");
 //if (timeNow > period18) goto link19;
 link19:
 if (timeNow > DateTime.Parse("15:55:00") && timeNow < DateTime.Parse("16:40:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period19 = DateTime.Parse("16:40:00");
 //if (timeNow > period19) goto link20;
 link20:
 if (timeNow > DateTime.Parse("16:40:00") && timeNow < DateTime.Parse("16:45:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period20 = DateTime.Parse("16:45:00");
 //if (timeNow > period20) goto link21;
 link21:
 if (timeNow > DateTime.Parse("16:45:00") && timeNow < DateTime.Parse("17:30:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period21 = DateTime.Parse("17:30:00");
 //if (timeNow > period21) goto link22;
 link22:
 if (timeNow > DateTime.Parse("17:30:00") && timeNow < DateTime.Parse("17:35:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period22 = DateTime.Parse("17:35:00");
 //if (timeNow > period22) goto link23;
 link23:
 if (timeNow > DateTime.Parse("17:35:00") && timeNow < DateTime.Parse("18:20:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period23 = DateTime.Parse("18:20:00");
 //if (timeNow > period23) goto link24;
 link24:
 if (timeNow > DateTime.Parse("18:20:00") && timeNow < DateTime.Parse("18:25:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period24 = DateTime.Parse("18:25:00");
 //if (timeNow > period24) goto link25;
 link25:
 if (timeNow > DateTime.Parse("18:25:00") && timeNow < DateTime.Parse("19:10:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period25 = DateTime.Parse("19:10:00");
 //if (timeNow > period25) goto link26;
 link26:
 if (timeNow > DateTime.Parse("19:10:00") && timeNow < DateTime.Parse("19:15:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period26 = DateTime.Parse("19:15:00");
 //if (timeNow > period26) goto link27;
 link27:
 if (timeNow > DateTime.Parse("19:15:00") && timeNow < DateTime.Parse("20:00:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period27 = DateTime.Parse("20:00:00");
 //if (timeNow > period27) goto link28;
 link28:
 if (timeNow > DateTime.Parse("20:00:00") && timeNow < DateTime.Parse("20:05:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period28 = DateTime.Parse("20:05:00");
 //if (timeNow > period28) goto link29;
 link29:
 if (timeNow > DateTime.Parse("20:05:00") && timeNow < DateTime.Parse("20:50:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period29 = DateTime.Parse("20:50:00");
 //if (timeNow > period29) goto link30;
 link30:
 if (timeNow > DateTime.Parse("20:50:00") && timeNow < DateTime.Parse("00:00:00"));
    {
      label1.ForeColor = Color.Red;
    }
   			var period30 = DateTime.Parse("00:00:00");
 //if (timeNow > period30) goto link31;
 link31:
 if (timeNow > DateTime.Parse("00:00:00") && timeNow < DateTime.Parse("08:15:00"));
    {
      label1.ForeColor = Color.Green;
    }
   			var period31 = DateTime.Parse("08:15:00");
 //if (timeNow > period31) goto link1;
 
 
    }
    
  private void MainFormLoad(object sender, EventArgs e)
 {
     timer.Interval = 1000;  
     timer.Tick += new EventHandler(Timer1Tick);
     timer.Start();          
 }
    }
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Bogdanov, 2018-09-27
@konoplinovich

Although I am not a real welder, I can advise the following: collect all the intervals in some kind of table or dictionary, and then in a separate method in the loop check if the current time falls into the intervals. When it hits, return the color value from the method.
More or less like this:

public class Interval
{
    DateTime LeftBound { get; set; }
    DateTime RightBound { get; set; }

    public Interval(DateTime left, DateTime right)
    {
       LeftBound = left;
       RightBound = right;
    }
}

...

List<Interval> intervals = new List<Interval>
{
    new Interval(DateTime.Parse("12:00:00"), DateTime.Parse("13:00:00")),
    new Interval(DateTime.Parse("14:00:00"), DateTime.Parse("20:00:00"))
};

...

public Color CheckInterval(DateTime now, List<Interval> timeTable)
{
   Color color = Color.Green;

   foreach (Interval interval in timeTable)
   {
      if (now > interval.LeftBound && now < interval.RightBound) 
      {
         color = Color.Red;
         break;
      }
   }

   return color;
}

...

label1.ForeColor = CheckInterval(DateTime.Now, intervals);

Put these pieces together and you should be good to go.
If someone smarter offers corrections - it will be cool!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question