rltcounter221 rltcounter221 - 2 months ago 11
C# Question

How to measure time between clicks in C#?

Hello so I want to make a code that does this. I keep clicking and if the time between clicks is >= 2000ms then write something in label else keep clicking.

Stopwatch sw = new Stopwatch();
double tt = 2000;
double duration = sw.ElapsedMilliseconds;

private void button1_Click(object sender, EventArgs e)
{

sw.Start();
if (duration >= tt)
{
label1.Text = "Speed reached!";
}
else
{
sw.Stop();
duration = 0;
}
}

Answer

If I understand your question correctly you want something like this:

Stopwatch sw = new Stopwatch();
double tt = 2000;

private void button1_Click(object sender, EventArgs e)
{
    sw.Stop();
    if (sw.ElapsedMilliseconds >= tt)
    {
        label1.Text = "Speed reached!";
    }
    sw.Reset();
    sw.Start();
}

This will start a stopwatch on the first click and then on each click it will measure the time between the clicks.

Comments