刷新计时器 c# Windows Forms
Refresh timer c# Windows Forms
嗨,我需要在每次按下“开始”按钮时将计时器值刷新为 0。请帮忙
这是我的代码:
namespace Timer
{
public partial class Form1 : Form
{
int counter = 0;
bool counting = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer.Interval = 1000;
}
private void btnStart_Click(object sender, EventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
btnStart.Text = "START";
lbTime.Items.Add("Time:" + lbCounter.Text);
lbCounter.Text = "";
}
else
{
timer.Start();
btnStart.Text = "STOP";
}
}
private void timer_Tick(object sender, EventArgs e)
{
Count();
}
private void Count()
{
counter++;
lbCounter.Text = counter.ToString();
}
}
}
当我按下开始按钮时,计时器开始从 0 开始计数。当我再次按下它时,它停止。第三次它应该再次从 0 开始,但它从上次停止的地方继续。
停止计时器时,只需将计数器设置为 0:
private void btnStart_Click(object sender, EventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
btnStart.Text = "START";
lbTime.Items.Add("Time:" + lbCounter.Text);
counter = 0;
lbCounter.Text = "";
}
else
{
timer.Start();
btnStart.Text = "STOP";
}
}
if (timer.Enabled)
{
timer.Stop();
btnStart.Text = "START";
lbTime.Items.Add("Time:" + lbCounter.Text);
lbCounter.Text = "";
}
else
{
counter = 0; // Added code.
timer.Start();
btnStart.Text = "STOP";
}
嗨,我需要在每次按下“开始”按钮时将计时器值刷新为 0。请帮忙
这是我的代码:
namespace Timer
{
public partial class Form1 : Form
{
int counter = 0;
bool counting = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer.Interval = 1000;
}
private void btnStart_Click(object sender, EventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
btnStart.Text = "START";
lbTime.Items.Add("Time:" + lbCounter.Text);
lbCounter.Text = "";
}
else
{
timer.Start();
btnStart.Text = "STOP";
}
}
private void timer_Tick(object sender, EventArgs e)
{
Count();
}
private void Count()
{
counter++;
lbCounter.Text = counter.ToString();
}
}
}
当我按下开始按钮时,计时器开始从 0 开始计数。当我再次按下它时,它停止。第三次它应该再次从 0 开始,但它从上次停止的地方继续。
停止计时器时,只需将计数器设置为 0:
private void btnStart_Click(object sender, EventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
btnStart.Text = "START";
lbTime.Items.Add("Time:" + lbCounter.Text);
counter = 0;
lbCounter.Text = "";
}
else
{
timer.Start();
btnStart.Text = "STOP";
}
}
if (timer.Enabled)
{
timer.Stop();
btnStart.Text = "START";
lbTime.Items.Add("Time:" + lbCounter.Text);
lbCounter.Text = "";
}
else
{
counter = 0; // Added code.
timer.Start();
btnStart.Text = "STOP";
}