I created a DataGridView (Building queue) which can be filled by the user. The user has to insert the time [Textbox H and M] it has to wait. After selecting the buildings and adding them to the queue, the user can press start [Button Start/Stop] and the countdown still start for a maximum of 4 buildings at a time [Textbox named Builders].
Pressed Start. 4 countdowns running. 5th one is waiting.
After the first timer hits zero, the row of that timer will be deleted. The program isn't using the right timers. (There was a bit of delay between the screenshots.) The first row has been duplicated. The first row timer has stopped working. and the others are still counting down. But the 5th row, disappeared.
I want the program to be able to continue with the next building countdown whenever one of the four countdowns is finished.
public void TimerFirstRow_Tick(object sender, EventArgs e)
{
if (time0 > 0 && this.dataGridView1.Rows.Count > 0)
{
time0--;
this.dataGridView1.Rows[0].Cells[2].Value = string.Format("00:{0}:{1}", time0 / 60, time0 % 60);
}
else
{
timer_0.Stop();
}
And I think here is the mistake / error, the place of the countdown is hardcoded into the Timer Tick event code.
The start button code:
private void StartButton_Click(object sender, EventArgs e)
{
QueueMax = this.dataGridView1.Rows.Count;
if (MaxBuild.Text == "")
{
label3.Text = "Fill MaxBuilders:";
}
else
{
MaxBuilder = int.Parse(MaxBuild.Text);
}
if (QueueMax >= MaxBuilder)
{
if (!timerB)
{
if (QueueMax > 0)
{
if (QueueMax == 1)
{
string xycoord = this.dataGridView1.Rows[0].Cells[3].Value.ToString();
string[] xyList = xycoord.Split(':');
int x = int.Parse(xyList[0]);
int y = int.Parse(xyList[1]);
this.WindowState = FormWindowState.Minimized;
Thread.Sleep(500);
SendKeys.Send("%+{TAB}");
Klikken(x, y);
this.WindowState = FormWindowState.Normal;
time0 = Convert.ToInt32(this.dataGridView1.Rows[0].Cells[2].Value) * 60;
timer_0 = new DispatcherTimer();
timer_0.Interval = new TimeSpan(0, 0, 1);
timer_0.Tick += TimerFirstRow_Tick;
timer_0.Start();
StartButton.Text = "Stop";
timerB = true;
}
else if (QueueMax == 2)
{
string xycoord = this.dataGridView1.Rows[0].Cells[3].Value.ToString();
string[] xyList = xycoord.Split(':');
int x = int.Parse(xyList[0]);
int y = int.Parse(xyList[1]);
this.WindowState = FormWindowState.Minimized;
Thread.Sleep(500);
SendKeys.Send("%+{TAB}");
Klikken(x, y);
string xycoord1 = this.dataGridView1.Rows[1].Cells[3].Value.ToString();
string[] xyList1 = xycoord.Split(':');
int x1 = int.Parse(xyList[0]);
int y1 = int.Parse(xyList[1]);
Klikken(x1, y1);
this.WindowState = FormWindowState.Normal;
time0 = Convert.ToInt32(this.dataGridView1.Rows[0].Cells[2].Value) * 60;
time1 = Convert.ToInt32(this.dataGridView1.Rows[1].Cells[2].Value) * 60;
timer_0 = new DispatcherTimer();
timer_1 = new DispatcherTimer();
timer_0.Interval = new TimeSpan(0, 0, 1);
timer_1.Interval = new TimeSpan(0, 0, 1);
timer_0.Tick += TimerFirstRow_Tick;
timer_1.Tick += timer2_Tick_1;
timer_0.Start();
timer_1.Start();
StartButton.Text = "Stop";
timerB = false;
}
else if (QueueMax == 3)
{
string xycoord = this.dataGridView1.Rows[0].Cells[3].Value.ToString();
string[] xyList = xycoord.Split(':');
int x = int.Parse(xyList[0]);
int y = int.Parse(xyList[1]);
this.WindowState = FormWindowState.Minimized;
Thread.Sleep(500);
SendKeys.Send("%+{TAB}");
Klikken(x, y);
string xycoord1 = this.dataGridView1.Rows[1].Cells[3].Value.ToString();
string[] xyList1 = xycoord.Split(':');
int x1 = int.Parse(xyList[0]);
int y1 = int.Parse(xyList[1]);
Klikken(x1, y1);
string xycoord2 = this.dataGridView1.Rows[2].Cells[3].Value.ToString();
string[] xyList2 = xycoord.Split(':');
int x2 = int.Parse(xyList[0]);
int y2 = int.Parse(xyList[1]);
Klikken(x1, y1);
this.WindowState = FormWindowState.Normal;
time0 = Convert.ToInt32(this.dataGridView1.Rows[0].Cells[2].Value) * 60;
time1 = Convert.ToInt32(this.dataGridView1.Rows[1].Cells[2].Value) * 60;
time2 = Convert.ToInt32(this.dataGridView1.Rows[2].Cells[2].Value) * 60;
timer_0 = new DispatcherTimer();
timer_1 = new DispatcherTimer();
timer_2 = new DispatcherTimer();
timer_0.Interval = new TimeSpan(0, 0, 1);
timer_1.Interval = new TimeSpan(0, 0, 1);
timer_2.Interval = new TimeSpan(0, 0, 1);
timer_0.Tick += TimerFirstRow_Tick;
timer_1.Tick += timer2_Tick_1;
timer_2.Tick += timer3_Tick;
timer_0.Start();
timer_1.Start();
timer_2.Start();
StartButton.Text = "Stop";
timerB = false;
}
else if (QueueMax >= 4)
{
string xycoord = this.dataGridView1.Rows[0].Cells[3].Value.ToString();
string[] xyList = xycoord.Split(':');
int x = int.Parse(xyList[0]);
int y = int.Parse(xyList[1]);
this.WindowState = FormWindowState.Minimized;
Klikken(x, y);
string xycoord1 = this.dataGridView1.Rows[1].Cells[3].Value.ToString();
string[] xyList1 = xycoord.Split(':');
int x1 = int.Parse(xyList[0]);
int y1 = int.Parse(xyList[1]);
Thread.Sleep(500);
SendKeys.Send("%+{TAB}");
Klikken(x1, y1);
string xycoord2 = this.dataGridView1.Rows[2].Cells[3].Value.ToString();
string[] xyList2 = xycoord.Split(':');
int x2 = int.Parse(xyList[0]);
int y2 = int.Parse(xyList[1]);
Klikken(x1, y1);
string xycoord3 = this.dataGridView1.Rows[3].Cells[3].Value.ToString();
string[] xyList3 = xycoord.Split(':');
int x3 = int.Parse(xyList[0]);
int y3 = int.Parse(xyList[1]);
Klikken(x1, y1);
this.WindowState = FormWindowState.Normal;
time0 = Convert.ToInt32(this.dataGridView1.Rows[0].Cells[2].Value) * 60;
time1 = Convert.ToInt32(this.dataGridView1.Rows[1].Cells[2].Value) * 60;
time2 = Convert.ToInt32(this.dataGridView1.Rows[2].Cells[2].Value) * 60;
time3 = Convert.ToInt32(this.dataGridView1.Rows[3].Cells[2].Value) * 60;
timer_0 = new DispatcherTimer();
timer_1 = new DispatcherTimer();
timer_2 = new DispatcherTimer();
timer_3 = new DispatcherTimer();
timer_0.Interval = new TimeSpan(0, 0, 1);
timer_1.Interval = new TimeSpan(0, 0, 1);
timer_2.Interval = new TimeSpan(0, 0, 1);
timer_3.Interval = new TimeSpan(0, 0, 1);
timer_0.Tick += TimerFirstRow_Tick;
timer_1.Tick += timer2_Tick_1;
timer_2.Tick += timer3_Tick;
timer_3.Tick += timer4_Tick;
timer_0.Start();
timer_1.Start();
timer_2.Start();
timer_3.Start();
StartButton.Text = "Stop";
timerB = false;
}
}
}
else
{
if (timer_0.IsEnabled)
{
timer_0.Stop();
timerB = false;
StartButton.Text = "Start";
} else if (timer_1.IsEnabled)
{
timer_0.Stop();
timer_1.Stop();
timerB = false;
StartButton.Text = "Start";
} else if (timer_2.IsEnabled)
{
timer_0.Stop();
timer_1.Stop();
timer_2.Stop();
timerB = false;
StartButton.Text = "Start";
} else if (timer_3.IsEnabled)
{
timer_0.Stop();
timer_1.Stop();
timer_2.Stop();
timer_3.Stop();
timerB = false;
StartButton.Text = "Start";
}
}
}
} //Start Timers