使用 Timers.Timer 来创建计时器。我使用 Timer.Elapsed 来处理事件,在特定时间后显示时间。我将计时器间隔设为 1,并启用 AutoReset。但问题是事件仅触发一次,我只能在文本框中获取一次时间。如何每秒更改文本框中的时间?我尝试了所有替代方法,但没有成功...谢谢。
System.Timers.Timer StopWatchTimer = new System.Timers.Timer();
Stopwatch sw = new Stopwatch();
public void StopwatchStartBtn_Click(object sender, ImageClickEventArgs e)
{
StopWatchTimer.Start();
StopWatchTimer.Interval = 1;
StopWatchTimer.Enabled = true;
StopWatchTimer.AutoReset =true;
sw.Start();
StopWatchTimer.Elapsed += new System.Timers.ElapsedEventHandler(StopWatchTimer_Tick);
}
protected void StopWatchStopBtn_Click(object sender, ImageClickEventArgs e)
{
TextBoxStopWatch.Text = "00:00:000";
StopWatchTimer.Stop();
sw.Reset();
sw.Stop();
}
public void StopWatchTimer_Tick(object sender,EventArgs e)
{
TextBoxStopWatch.Text= Convert.ToString(sw.Elapsed);
}
更新: 我尝试通过在Visual Studio中创建新的网站来解决问题,但仍然没有成功。问题仍然存在。现在的更新是当我在代码的某一行设置断点时
TextBoxStopWatch.Text= Convert.ToString(sw.Elapsed);
文本在不断变化,但没有显示在文本框中。希望您能理解这一点。