亚历克斯2020
新成员
- 已加入
- 2020年10月13日
- 留言内容
- 3
- 编程经验
- Beginner
你好,
我是C#的新手。我试图做一个秒表。我的问题是,我使用1 ms的间隔值时,无法将计时器与1/1000秒秒表匹配。
ms在10秒内达到1000,而必须在1s内达到。我该如何解决这个问题?
我的代码是:
我是C#的新手。我试图做一个秒表。我的问题是,我使用1 ms的间隔值时,无法将计时器与1/1000秒秒表匹配。
ms在10秒内达到1000,而必须在1s内达到。我该如何解决这个问题?
我的代码是:
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;
}
}
}