For the project I'm working on there is a label, start button, stop button, reset button, and a timer control. When the start button is clicked it starts the elapsed time. When the stop button is pressed the elapsed time should stop, which it does. Once I hit the start button again (after the stop button is pressed) the elapsed time starts over. I need to be able to hit the start button and resume the elapsed time where it left off. Here is some of my code:
- public partial class Form1 : Form
-
{
-
DateTime startTime = new DateTime();
-
TimeSpan elapsedTime = new TimeSpan();
-
public Form1()
-
{
-
InitializeComponent();
-
-
}
-
-
private void startBtn_Click(object sender, EventArgs e)
-
{
-
startBtn.Enabled = false;
-
stopBtn.Enabled = true;
-
resetBtn.Enabled = true;
-
timer1.Interval = (1000) * (1);
-
timer1.Enabled = true;
-
timer1.Start();
-
startTime = DateTime.Now;
-
-
-
}
-
-
private void stopBtn_Click(object sender, EventArgs e)
-
{
-
stopBtn.Enabled = false;
-
startBtn.Enabled = true;
-
resetBtn.Enabled = true;
-
timer1.Stop();
-
-
}
-
-
private void timer1_Tick(object sender, EventArgs e)
-
{
-
-
elapsedTime = DateTime.Now - startTime;
-
label1.Text = elapsedTime.ToString();
-
}
-
}
I'm not sure how to use the elapsed time variable in a if statement to check its value. The elapsed time variable is set up as a timespan. I also would like the elapsed time to display as 00.00.00 and I'm not sure how to go about this.
Can some help with some ideas?
Thanks!