C#倒计时程序怎么停止timer
来源:百度知道 编辑:UC知道 时间:2024/07/02 16:00:10
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = ((Convert.ToInt32(this.label1.Text) - 1)).ToString();
if (this.label1.Text == "0") { MessageBox.Show("倒计时结束"); this.timer1.Enabled = 1; }
}
}
}
这个程序从10倒计时到0后继续-1,都成负数了。我想在if (this.label1.Text == "0") { MessageBox.Show("倒计时结束"); this.timer1.Enabled = 1; }里加一个this.timer1.Stop();结果还是减成负数了,请问怎么办啊?
fsxy1226:你的答案试了,还是不行。
kid
呵呵,问题就出在你的MessageBox.Show("倒计时结束")上,换下顺序就可以了。不换你就用最快的速度点确定。呵呵呵呵。
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = ((Convert.ToInt32(this.label1.Text) - 1)).ToString();
if (this.label1.Text == "0")
{
this.timer1.Stop();
MessageBox.Show("倒计时结束");
}
}
========
另外C#不能用int来表示bool,this.timer1.Enabled = 1肯定编译不过。
this.timer1.Enabled = false
private static int i = 10;
private void btnShow_Click(object sender, EventArgs e)
{
this.timer1.Interval = 1000;
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = i.ToString();
if (i == 0)
{
this.