I have created a windows form application in c# in which input from user is taken .I want to calculate time spent by user in between two submissions.How can I do that? The buttons are on different windows forms.
Asked
Active
Viewed 84 times
0
-
1set a time counter activated by one button and closed by another. – AmeRyoki Mar 23 '17 at 12:18
-
3Possible duplicate of [How to track time between two button clicks in C# in a Windows form application?](http://stackoverflow.com/questions/13048857/how-to-track-time-between-two-button-clicks-in-c-sharp-in-a-windows-form-applica) – AmeRyoki Mar 23 '17 at 12:25
-
@Jane dow How do I stop the stopwatch on another form.The question which you are referring has both buttons on same form. – ItaliaIshan Mar 23 '17 at 12:57
-
global variable like `timerInProgress = true/false` – AmeRyoki Mar 23 '17 at 13:21
1 Answers
2
public TimeSpan ts = new TimeSpan();
public Stopwatch = new Stopwatch();
public void triggerTimer()
{
if (StopWatch.IsRunning)
{
stopWatch.Stop();
ts = stopWatch.Elapsed;
}
else stopWatch.Start();
}
private void button1_Click(object sender, System.EventArgs e)
{
triggerTimer();
//your code
}
}
private void button2_Click(object sender, System.EventArgs e)
{
triggerTimer();
//your code
}
Read more about StopWatch.

AmeRyoki
- 376
- 1
- 3
- 14