Доброго времени суток! Прошу помощи в понимании моих ошибок -
есть цель создать часы, меняющие цвет (Красный или Зеленый) в зависимости от текущего промежутка времени (по заданному расписанию).
Желательно без привязки к дате, то есть после запуска программы она может функционировать более 24 часов, нон-стопом так сказать.
Ниже приведен мой код. Не могу сам дойти до правильного решения, понимаю, что нужно сделать постоянную сверку текущего времени с расписанием, а получается только последнее условие принимается во внимание и смены цвета далее не происходит. Помогите разобраться и понять алгоритм действий.
Заранее благодарю!
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();
}
}
}