I have a query here, I want the start and stop to be the same button and I want to display the time that tells me how many seconds it took me to start and stop. I am unable to do that seeking your help. here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Timers4
{
public partial class TimingGameForm : Form
{
public TimingGameForm()
{
InitializeComponent();
}
private int elapsed = 0;
private bool started = false;
private void Start_Click(object sender, EventArgs e)
{
if (started == true)
{
timer1.Enabled = true;
Estimatelbl.Text = "Running....";
}
else if (started == false)
{
timer1.Enabled = false;
Timelbl.Text = "that took" + elapsed + "seconds";
}
}
private void timer_Tick(object sender, EventArgs e)
{
elapsed += 1;
}
}
}
Aaron ColenPosted Oct 11, 2010, 4:12 PM
In your if/elseif you need to set started to true/false.
Then you want to add a DateTime variable to store the beginning time. Then you can calculate a TimeSpan as part of your 'stop' routine.
sudha kovid kolliPosted Oct 11, 2010, 6:45 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Timers4
{
public partial class TimingGameForm : Form
{
public TimingGameForm()
{
InitializeComponent();
}
private int elapsed = 0;
private bool started = false;
System.DateTime x;
private void Start_Click(object sender, EventArgs e)
{
if (started == true)
{
started = true;
timer1.Enabled = true;
Estimatelbl.Text = "Running....";
}
else if (started == false)
{
started = false;
timer1.Enabled = false;
Timelbl.Text = "that took" + elapsed + "seconds";
}
}
private void timer_Tick(object sender, EventArgs e)
{
elapsed += 1;
}
}
}