[英]How can I make the time counter to continue up/down from the current point and not from the start?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Extract
{
public partial class TimeCounter : Label
{
public bool CountUp { get; set; }
private Timer _timer;
private int _elapsedSeconds;
private TimeSpan ts = TimeSpan.FromSeconds(100);
public TimeCounter()
{
InitializeComponent();
StartCountDownTimer();
}
public void StartCountDownTimer()
{
_timer = new Timer
{
Interval = 1000,
Enabled = true
};
_timer.Tick += (sender, args) =>
{
if (CountUp == false)
{
ts = ts.Subtract(TimeSpan.FromSeconds(1));
this.Text = ts.ToString();
}
else
{
_elapsedSeconds++;
TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
this.Text = time.ToString(@"hh\:mm\:ss");
}
};
}
private void TimeCounter_Load(object sender, EventArgs e)
{
}
}
}
And in form1在form1中
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
timeCounter1.CountUp = true;
}
else
{
timeCounter1.CountUp = false;
}
}
When I change the CountUp flag in form1 it's changing the time counter direction up/down but it's starting over each time.当我在 form1 中更改 CountUp 标志时,它会向上/向下更改时间计数器方向,但每次都会重新开始。 Id it's counting up then it start from 00:00:00 and if counting down then from 1 minutes and 40 seconds 00:01:40 Id 它正在计数,然后从 00:00:00 开始,如果倒计时,则从 1 分 40 秒 00:01:40
How can I make that when I change the flag it will change the direction from the current time and not from the start ?当我更改标志时,我怎样才能做到这一点,它会从当前时间而不是从一开始就改变方向?
If the time is for example 00:00:13 (counting up) and I change the flag to count down then count down from 00:00:13 ... 00:00:12... same the other way if it's counting down and I change it to count up then continue up from the current time.例如,如果时间是 00:00:13(向上计数)并且我将标志更改为向下计数,则从 00:00:13 ... 00:00:12 向下计数...如果正在计数,则以另一种方式进行计数向下,我将其更改为向上计数,然后从当前时间继续向上。
What do you need "_elapsedSeconds" for?你需要“_elapsedSeconds”做什么?
Just use?就用?
_timer.Tick += (sender, args) =>
{
if (CountUp)
{
ts = ts.Add(TimeSpan.FromSeconds(1));
}
else
{
ts = ts.Subtract(TimeSpan.FromSeconds(1));
}
this.Text = ts.ToString();
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.