using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;using System.Timers;namespace Time { public partial class Form1 : Form { private int second=0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { timer1.Enabled = true; timer1.Interval = 1000; second =Convert .ToInt32(textBox1.Text)*60; } private void timer1_Tick(object sender, EventArgs e) { if (second >= 0) { label1.Text = second.ToString(); second--; if (second == 5) { MessageBox.Show("时间到.请交试卷"); } } } } }
WinForm中用Timer控件
你先定义一个倒计时的时间,比如说10分钟,然后记录当前时间,每一秒钟时间就减1,然后直到为零,就倒计时结束了
变通一下,用减法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Timers;namespace Time
{
public partial class Form1 : Form
{
private int second=0; public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 1000;
second =Convert .ToInt32(textBox1.Text)*60;
} private void timer1_Tick(object sender, EventArgs e)
{
if (second >= 0)
{
label1.Text = second.ToString();
second--;
if (second == 5)
{
MessageBox.Show("时间到.请交试卷");
}
}
}
}
}