Alex2020
New member
- Joined
- Oct 13, 2020
- Messages
- 3
- Programming Experience
- Beginner
Hi,
I'm new to C#. I tried to make a stopwatch. My problem is that I cannot match the timer with 1/1000 s stopwatch, while I have used interval value of 1 ms.
The ms reaches to 1000 in 10s while it must reach in 1s. How can I solve this issue?
My code is:
I'm new to C#. I tried to make a stopwatch. My problem is that I cannot match the timer with 1/1000 s stopwatch, while I have used interval value of 1 ms.
The ms reaches to 1000 in 10s while it must reach in 1s. How can I solve this issue?
My code is:
Stopwatch:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Stopwatch
{
public partial class Form1 : Form
{
int TimeCCs, TimeCs, TimeSec, TimeMin;
bool IsActive;
public Form1()
{
InitializeComponent();
}
private void btnStop_Click(object sender, EventArgs e)
{
IsActive = false;
}
private void btnReset_Click(object sender, EventArgs e)
{
IsActive = false;
ResetTime();
}
private void ResetTime()
{
TimeCCs = 0;
TimeCs = 0;
TimeSec = 0;
TimeMin = 0;
//IsActive = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (IsActive)
{
timer1.Start();
timer1.Interval = 1;
TimeCCs++;
if (TimeCCs >= 1000)
{
TimeCs++;
TimeCCs = 0;
}
if (TimeCs >= 100)
{
TimeSec++;
TimeCs = 0;
if (TimeSec >= 60)
{
TimeMin++;
TimeSec = 0;
}
}
}
DrawTime();
}
private void DrawTime()
{
LabelCCs.Text = string.Format("{0:000}", TimeCCs);
LabelCs.Text = string.Format("{0:00}",TimeCs);
LabelMin.Text = string.Format("{0:00}", TimeMin);
LabelSec.Text = string.Format("{0:00}", TimeSec);
}
private void btnStart_Click(object sender, EventArgs e)
{
IsActive = true;
}
private void Form1_Load(object sender, EventArgs e)
{
ResetTime();
IsActive = false;
}
}
}