J'ai un lblCountdown avec une valeur int de 60. Je veux que la valeur int du lblCountDown diminue avec des secondes jusqu'à atteindre 0.
C'est ce que j'ai jusqu'ici:
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();
}
Utiliser la minuterie pour cela
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();
}
int segundo = 0;
DateTime dt = new DateTime();
private void timer1_Tick(object sender, EventArgs e){
segundo++;
label1.Text = dt.AddSeconds(segundo).ToString("HH:mm:ss");
}
Vous devrez utiliser un minuteur et vous connecter à l'événement Tick pour faire ce que vous cherchez . http://msdn.Microsoft.com/en-us/library/system.windows.forms timer.aspx
Comment utiliser la classe Timer de .NET Framework? http://msdn.Microsoft.com/en-us/library/0tcs6ww8.aspx
Vous avez besoin d'une classe publique pour que Form1 s'initialise.
Voir ce code:
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
}
}
}
J'ai essayé et tout a bien fonctionné
Si vous avez besoin d'aide, n'hésitez pas à commenter :)
Usage:
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;
et n'oubliez pas de Dispose();
quand la minuterie est inutile pour vous;
Code source:
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();
}