Ok, so i'm trying to get back into the swing of C# and it's slowly coming back to me but I have a slight problem. I'm making a simple stop watch program that is supposed to print the time as it ticks in a windows form app. It's as simple as:
public partial class Form1 : Form
Stopwatch stopWatch = new Stopwatch();
private void start_Click(object sender, EventArgs e)
private void stop_Click(object sender, EventArgs e)
private void reset_Click(object sender, EventArgs e)
stopWatch = Stopwatch.StartNew();
private void timePrint()
label1.Text = stopWatch.ElapsedTicks.ToString();
hours.Text = "Time elapsed: " + stopWatch.Elapsed;
It doesn't quite update the label as the stopwatch counts though. I can do it in as a CMD line app but I didn't want to do the application in cmd line form. Any suggestions?
If I use the timer object will I still need the Stowatch() class?
Probably yes because it serves a different responsibility. The stopwatch class would contain the current timing and allow you to format the display as you which. The timer exists solely to update the display.
If you only used the timer, then the accuracy may suffer. This is because user interaction (such as moving the mouse) could interfere with the timer click. If you use the stopwatch the elapsed time will be correct even though the display update may not happen on precisely every interval (i.e for a 100ms resolution, the actual timer click may be at 101ms or 104ms, etc).