我有一个int值为60的lblCountdown。我想让lblCountDown的int值减半,直到达到0。
这是我到目前为止所做的:
private int counter = 60;
private void button1_Click(object sender, EventArgs e)
{
int counter = 60;
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start();
label1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if (counter == 0)
timer1.Stop();
label1.Text = counter.ToString();
}
答案 0 :(得分:17)
使用计时器
private System.Windows.Forms.Timer timer1;
private int counter = 60;
private void btnStart_Click_1(object sender, EventArgs e)
{
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start();
lblCountDown.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if (counter == 0)
timer1.Stop();
lblCountDown.Text = counter.ToString();
}
答案 1 :(得分:3)
Product
答案 2 :(得分:1)
你需要使用一个Timer并挂钩Tick事件来做你想要的事情。 http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
答案 3 :(得分:1)
用法:
CountDownTimer timer = new CountDownTimer();
//set to 30 mins
timer.SetTime(30,0);
timer.Start();
//update label text
timer.TimeChanged += () => Label1.Text = timer.TimeLeftMsStr;
// show messageBox on timer = 00:00.000
timer.CountDownFinished += () => MessageBox.Show("Timer finished the work!");
//timer step. By default is 1 second
timer.StepMs = 33;
并且当计时器对您无用时,请不要忘记Dispose();
;
源代码:
using System;
using System.Windows.Forms;
public class CountDownTimer : IDisposable
{
public Action TimeChanged;
public Action CountDownFinished;
public bool IsRunnign => timer.Enabled;
public int StepMs
{
get => timer.Interval;
set => timer.Interval = value;
}
private Timer timer = new Timer();
private DateTime _maxTime = new DateTime(1, 1, 1, 0, 30, 0);
private DateTime _minTime = new DateTime(1, 1, 1, 0, 0, 0);
public DateTime TimeLeft { get; private set; }
private long TimeLeftMs => TimeLeft.Ticks / TimeSpan.TicksPerMillisecond;
public string TimeLeftStr => TimeLeft.ToString("mm:ss");
public string TimeLeftMsStr => TimeLeft.ToString("mm:ss.fff");
private void TimerTick(object sender, EventArgs e)
{
if (TimeLeftMs > timer.Interval)
{
TimeLeft = TimeLeft.AddMilliseconds(-timer.Interval);
TimeChanged?.Invoke();
}
else
{
Stop();
TimeLeft = _minTime;
TimeChanged?.Invoke();
CountDownFinished?.Invoke();
}
}
public CountDownTimer(int min, int sec)
{
SetTime(min, sec);
Init();
}
public CountDownTimer(DateTime dt)
{
SetTime(dt);
Init();
}
public CountDownTimer()
{
Init();
}
private void Init()
{
TimeLeft = _maxTime;
StepMs = 1000;
timer.Tick += new EventHandler(TimerTick);
}
public void SetTime(DateTime dt) {
TimeLeft = _maxTime = dt;
TimeChanged?.Invoke();
}
public void SetTime(int min, int sec=0) => SetTime(new DateTime(1, 1, 1, 0, min, sec));
public void Start() => timer.Start();
public void Pause() => timer.Stop();
public void Stop()
{
Pause();
Reset();
}
public void Reset()
{
TimeLeft = _maxTime;
}
public void Restart()
{
Reset();
Start();
}
public void Dispose() => timer.Dispose();
}
答案 4 :(得分:0)
如何使用.NET Framework中的Timer类? http://msdn.microsoft.com/en-us/library/0tcs6ww8.aspx
答案 5 :(得分:0)
您需要Form1的公共类进行初始化。
请参阅此代码:
namespace TimerApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter = 60;
private void button1_Click(object sender, EventArgs e)
{
//Insert your code from before
}
private void timer1_Tick(object sender, EventArgs e)
{
//Again insert your code
}
}
}
我试过这个并且一切正常
如果您需要帮助,请随时评论:)
答案 6 :(得分:-1)
嘿,请在您的项目中添加代码,这很容易,我想可以解决您的问题。
int count = 10;
private void timer1_Tick(object sender, EventArgs e)
{
count--;
if (count != 0 && count > 0)
{
label1.Text = count / 60 + ":" + ((count % 60) >= 10 ? (count % 60).ToString() : "0" + (count % 60));
}
else
{
label1.Text = "game over";
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 1;
timer1.Tick += new EventHandler(timer1_Tick);
}