Consider using the System.Diagnostics.Stopwatch
class. Call Start()
when the first button is clicked, and Stop()
when the second button is clicked. Then, the difference in minutes is Stopwatch.Elapsed.TotalMinutes;
In the code example you've now given you've declared two new
stopwatches that only exist within the scope of each method.
Declare it outside the methods like this:
Stopwatch stopWatch = new Stopwatch();
private void button1_Click(object sender, EventArgs e)
{
// get system time to the Start time
lblCurrentTime.Text = DateTime.Now.ToShortTimeString();
stopWatch.Start();
}
private void button2_Click(object sender, EventArgs e)
{
stopWatch.Stop();
textBox1.Text = DateTime.Now.ToShortTimeString();
lblCurrentPrice.Text = Stopwatch.Elapsed.TotalMinutes;
}