如何使时间计数器从当前点而不是从头开始继续 up/down?
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)
{
}
}
}
在 form1 中
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
timeCounter1.CountUp = true;
}
else
{
timeCounter1.CountUp = false;
}
}
当我更改 form1 中的 CountUp 标志时,它正在更改时间计数器的方向 up/down 但它每次都重新开始。如果它是向上计数,那么它从 00:00:00 开始,如果是向下计数,那么从 1 分 40 秒开始 00:01:40
我怎样才能做到当我改变标志时它会从当前时间而不是从一开始就改变方向?
如果时间是例如 00:00:13(向上计数),我将标志更改为向下计数,然后从 00:00:13...00:00:12... 相同其他方式,如果它正在倒计时,我将其更改为向上计数,然后从当前时间继续向上。
你需要“_elapsedSeconds”做什么?
随便用?
_timer.Tick += (sender, args) =>
{
if (CountUp)
{
ts = ts.Add(TimeSpan.FromSeconds(1));
}
else
{
ts = ts.Subtract(TimeSpan.FromSeconds(1));
}
this.Text = ts.ToString();
};
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)
{
}
}
}
在 form1 中
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
timeCounter1.CountUp = true;
}
else
{
timeCounter1.CountUp = false;
}
}
当我更改 form1 中的 CountUp 标志时,它正在更改时间计数器的方向 up/down 但它每次都重新开始。如果它是向上计数,那么它从 00:00:00 开始,如果是向下计数,那么从 1 分 40 秒开始 00:01:40
我怎样才能做到当我改变标志时它会从当前时间而不是从一开始就改变方向?
如果时间是例如 00:00:13(向上计数),我将标志更改为向下计数,然后从 00:00:13...00:00:12... 相同其他方式,如果它正在倒计时,我将其更改为向上计数,然后从当前时间继续向上。
你需要“_elapsedSeconds”做什么?
随便用?
_timer.Tick += (sender, args) =>
{
if (CountUp)
{
ts = ts.Add(TimeSpan.FromSeconds(1));
}
else
{
ts = ts.Subtract(TimeSpan.FromSeconds(1));
}
this.Text = ts.ToString();
};