0

I just want to ask that if I can make a stopwatch with C# I tried:

        private void button2_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        int st = 00;
        int m = 00;

        string stime = "00:00";
        if(st == 60)
        {
            m++;
            st = 00;
        }
        else
        {
            st++;
        }
        if (m == 60)
        {
            m = 00;
        }
        if(st < 10)
        {
            st = 0 + st;
        }
        if(m < 10)
        {
            m = 0 + m;
        }
        stime = m.ToString() + ":" + st.ToString();
        label3.Text = stime;
    }

this but it didn't worked. My timer is setted up and the interval of the timer is 1000ms. Can someone help me?

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Doruk Ayar
  • 334
  • 1
  • 4
  • 17
  • Did you initialize the interval in your code? It is not in the code posted. – jdweng Sep 17 '16 at 08:09
  • I setted up the interval on Visual Studio Designer – Doruk Ayar Sep 17 '16 at 08:12
  • Is this just for learning purposes or is it for a code project? If it is a code project, then you should use the build in Stopwatch. If it is learning purposes I would recommend taking a look at the build in class: https://github.com/mono/mono/blob/master/mcs/class/System/System.Diagnostics/Stopwatch.cs – Kristian Barrett Sep 17 '16 at 08:13
  • Possible duplicate of [Stop watch delay using timer c#](http://stackoverflow.com/questions/31777476/stop-watch-delay-using-timer-c-sharp) – Waqas Shabbir Sep 17 '16 at 08:13
  • I avoid use the designer to set up properties because it isn't obvious what the value actually are set to. When debugging code you can't just look at one file to figure out the errors. Interval is in milliseconds (not seconds) so make sure you set the interval to the correct value. – jdweng Sep 17 '16 at 08:26
  • You can use a timer together with the [**Stopwatch class**](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx). Start the stopwatch and the timer at the same time, then let the timer update the label with the stopwatch's `Elapsed` property. Have the timer's `Interval` set to 1. – Visual Vincent Sep 17 '16 at 08:33
  • check http://stackoverflow.com/questions/11681457/set-start-time-for-stopwatch-program-in-c-sharp ... may be it could help – Vishal Goyal Sep 17 '16 at 08:34

1 Answers1

6

It looks to me, that you are more likly to make a watch rather than a stopwatch?

If you're making a stopwatch, I think you need a field/property in your class that holds the starting time:

private DateTime _start;
private void button2_Click(object sender, EventArgs e)
{
    _start = DateTime.Now;
    timer1.Start();
}

and then in timer1_Tick you can do:

private void timer1_Tick(object sender, EventArgs e)
{
    TimeSpan duration = DateTime.Now - _start;
    label3.Text = duration.ToString(<some format string>);
}

It seems that your current code in timer1_Tick only has local variables and therefore always will produce the same time? :-)

  • Visual Studio gives me an error. That says me undefined variable – Doruk Ayar Sep 17 '16 at 09:08
  • @DorukAyar: but you'll have to declare _start as a field of the class you have the button2_Click() and timer1_Tick() procedures in as I show above in the first line of code. –  Sep 17 '16 at 09:13