I've done the difficult dailyprogrammer challenge #2:
Your mission is to create a stopwatch program. this program should have start, stop, and lap options, and it should write out to a file to be viewed later.
I wanted to see from you guys if what I did was correct. I'm looking for shortcuts or better coding practices I could have missed. This is a Windows Form Application.
namespace _Hard0002_StopWatch {
public partial class StopWatchForm : Form {
StateEnum state;
Stopwatch stopWatch;
TimeSpan timeSpan;
string elapsedTime;
List<string> times = new List<string>();
public StopWatchForm() {
InitializeComponent();
state = StateEnum.Stoped;
}
private void StartAndLapButton_Click(object sender, EventArgs e) {
if (state == StateEnum.Stoped) {
stopWatch = Stopwatch.StartNew();
state = StateEnum.Started;
ChangeButtonText(StartAndLapButton, "Lap");
ChangeFormTittle("Started");
timer1.Enabled = true;
} else if (state == StateEnum.Started) {
times.Add(elapsedTime);
}
}
private void StopButton_Click(object sender, EventArgs e) {
if (state == StateEnum.Started) {
ChangeButtonText(StartAndLapButton, "Start");
ChangeFormTittle("Stop Watch");
stopWatch.Stop();
times.Add(elapsedTime);
state = StateEnum.Stoped;
timer1.Enabled = false;
}
}
private void FileButton_Click(object sender, EventArgs e) {
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (times.Count > -1) {
if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
string path = saveFileDialog1.FileName;
System.IO.File.WriteAllLines(path + ".txt", times);
times.Clear();
}
}
}
void ChangeButtonText(Button button, string text) {
button.Text = text;
}
private void ChangeFormTittle(string text) {
this.Text = text;
}
private void timer1_Tick(object sender, EventArgs e) {
timeSpan = stopWatch.Elapsed;
elapsedTime = String.Format("{0:00}:{1:00}:{2:00}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
TimeKeeperText.Text = elapsedTime;
}
///Your mission is to create a stopwatch program.
///this program should have start, stop, and lap options,
///and it should write out to a file to be viewed later.
///
///Nouns:
///Start
///lap
///stop
///file (txt)
///
///Verbs:
///Keeps time
///Writes file
///
}
}