public Form1() {
public Form1()
{
InitializeComponent();
dtStopwatches.Columns.Add("ID", typeof(int));
dtStopwatches.Columns.Add("Nombre", typeof(string));
dtStopwatches.Columns.Add("Tiempo Restante", typeof(string));
dataGridView1.DataSource = dtStopwatches;
foreach (string buttonName in new string[] { "Pausar/Reanudar", "Reset", "Restart" })
{
DataGridViewButtonColumn colTemp = new DataGridViewButtonColumn();
colTemp.Name = buttonName + "Col";
colTemp.HeaderText = buttonName;
colTemp.Width = 100;
dataGridView1.Columns.Add(colTemp);
}
timer1.Tick += (timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e) <----- AQUÍ EL PROBLEMA {
private void timer1_Tick(object sender, EventArgs e) <----- AQUÍ EL PROBLEMA
{
timer1.Stop();
foreach (DataRow dRow in dtStopwatches.Rows)
{
int hours = int.Parse((TimeSpan.Parse(swDct[(int)dRow["ID"]].Elapsed.ToString()) - TimeSpan.Parse(1.ToString())).ToString(@"hh")) - 23; // <----- ASIGNACIÓN DE HORAS (24 - horas deseadas)
int mins = int.Parse((TimeSpan.Parse(swDct[(int)dRow["ID"]].Elapsed.ToString()) - TimeSpan.Parse(1.ToString())).ToString(@"mm")) - 59; // <----- ASIGNACIÓN DE MINUTOS (60 - minutos deseados)
int secs = int.Parse(((TimeSpan.Parse(swDct[(int)dRow["ID"]].Elapsed.ToString()) - TimeSpan.Parse(1.ToString())).ToString(@"ss"))) - 55; // <------ ASIGNACIÓN DE SEGUNDOS (60 - segundos deseados)
if (hours == 0 && mins == 0 && secs == 0)
{
swDct[(int)dRow["ID"]].Stop();
//dtStopwatches.Rows.Remove(dRow); **<--- en este punto debería eliminarse el row**
}
string temp = hours.ToString("00") + ":"
+ mins.ToString("00") + ":"
+ secs.ToString("00");
dRow["Tiempo Restante"] = temp;
}
timer1.Start();
}
private void BtnAddStopwatch_Click_1(object sender, EventArgs e) {
private void BtnAddStopwatch_Click_1(object sender, EventArgs e)
{
dtStopwatches.Rows.Add(nextID, txt_nombre.Text, "00:00:00");
swDct.Add(nextID, new Stopwatch());
nextID++;
Stopwatch swTemp = swDct[(int)dataGridView1.Rows[temp].Cells["ID"].Value];
swTemp.Start();
temp++;
}
private void DataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
Stopwatch swTemp = swDct[(int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value];
switch (dataGridView1.Columns[e.ColumnIndex].HeaderText)
{
case "Pausar/Reanudar":
if (swDct[(int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value].IsRunning == true)
{ swTemp.Stop();}
else{
swTemp.Start();}
break;
case "Reset":
swTemp.Reset();
break;
case "Restart":
swTemp.Restart();
break;
}
}
}