How to track time between two button clicks in C# in a Windows form application?

前端 未结 3 1397
青春惊慌失措
青春惊慌失措 2021-01-29 00:06

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?

3条回答
  •  轮回少年
    2021-01-29 00:33

    Use Stopwatch. Create object of the Stopwatch at class level and use that to calculate time.

    Something like:

     public partial class Form1 : Form
        {
            Stopwatch stopwatch = new Stopwatch();
    
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                stopwatch.Start();
    
            }
            private void button2_Click(object sender, EventArgs e)
            {
    
                stopwatch.Stop();
                var milliSeocnds = stopwatch.ElapsedMilliseconds;
                var timeSpan = stopwatch.Elapsed;
            }
        }
    

提交回复
热议问题