-4

How to create a stopwatch in c# (visual studio 2012)that starts when you start typing in a a text box and stops when enter is pressed? It should start again when i start typing another word and end again on pressing enter, then display the times recorded for each word.

The following example demonstrates how to use the Stopwatch class to determine the execution time for an application. C#

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        Thread.Sleep(10000);
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);
    }
}
  • 2
    What specific aspect of it are you having trouble with? – rory.ap Mar 30 '16 at 11:56
  • So, based on this example, what have you tried? Where are you stuck? – David Mar 30 '16 at 11:57
  • What's wrong with what you have? – Guy Mar 30 '16 at 12:01
  • What kind of text box do you mean? I'm *assuming* Windows Forms. So you might want to take a look at the [KeyDown](https://msdn.microsoft.com/library/system.windows.forms.control.keydown.aspx) event (or another fitting one). -- Seems like you already know how to create/use a *stopwatch*, and have more of a problem with the other stuff. – Corak Mar 30 '16 at 12:02
  • @roryapwhat codes do i use to make the stopwatch start when i press a key on my keyboard (other than 'enter') and make it stop when i press enter. and then start again when i press another key and display the time each time i press enter. – karishma13 Mar 30 '16 at 12:06
  • @karishma13 - again, that has less to do with the *stopwatch* itself and more with how to react to key presses (key events). Follow the [KeyDown](https://msdn.microsoft.com/library/system.windows.forms.control.keydown.aspx) link for examples on how to do that. -- Looking at the [Keys enum](https://msdn.microsoft.com/library/system.windows.forms.keys.aspx) is probably helpful, too. – Corak Mar 30 '16 at 12:09

1 Answers1

0

You could override the StopWatch and make it fit your whishes like this:

private void Usage()
{
    WordStopWatch wsw = new WordStopWatch();

    // On textchanged?
    wsw.Start();

    // On keypress -> Enter
    wsw.Stop(/*Your control here ala*/ textBoxInput.Something);
}

private class WordStopWatch : Stopwatch
{
    // Temporary save start-time and end-time
    public DateTime StartAt { get; private set; }
    public DateTime EndAt { get; private set; }

    // Save your records
    public List<Tuple<TimeSpan, string>> Records;

    // Override Start() to save StartAt
    public void Start()
    {
        StartAt = DateTime.Now;
        base.Start();
    }

    // Override Stop() to save EndAt and your records.
    public void Stop(string word)
    {
        EndAt = DateTime.Now;
        StartAt = EndAt - base.Elapsed;
        base.Stop();

        Records.Add(new Tuple<TimeSpan, string>(EndAt - StartAt, word));
    }
}

This is just a push in one possible direction. With a bit effort you should be able to go on.

C4d
  • 3,183
  • 4
  • 29
  • 50