今天在网上看到别人写的秒表代码,自己为了练习一下timer控件的使用方法,也写了一个。代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestTimerControl2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
this.timerClock.Interval = 10;
}
private void timerClock_Tick(object sender, EventArgs e)
{
int minute = int.Parse(this.labelMinute.Text); //分
int second = int.Parse(this.labelSecond.Text); //秒
int milli = int.Parse(this.labelMilli.Text); //10毫秒,1s=1000ms
milli++;
if (milli == 100)
{
second++;
this.labelSecond.Text = second.ToString();
//milli = 0;
}
if (second == 60)
{
minute++;
this.labelMinute.Text = minute.ToString();
this.labelSecond.Text = "0";
if (minute == 100)
{
this.timerClock.Enabled = false;
MessageBox.Show("秒表已达到上限","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
}
milli = milli % 100;
this.labelMilli.Text = milli.ToString();
}
private void buttonBegin_Click(object sender, EventArgs e) //开始button
{
if (this.timerClock.Enabled == false)
{
this.timerClock.Enabled = true;
this.buttonBegin.Text = "停止";
}
else
{
this.timerClock.Enabled = false;
this.buttonBegin.Text = "开始";
}
}
private void buttonReset_Click(object sender, EventArgs e) //清零button
{
this.labelMinute.Text = "0";
this.labelSecond.Text = "0";
this.labelMilli.Text = "0";
}
}
}