显示时间跨度的标签在 XP 中消失,但在较新的 Windows 版本中不会消失
我有一个秒表计时器,已添加到我的程序中。它在我的 Win 7 机器和我尝试过的 Vista 机器上工作正常,但在 XP 中,当计时器启动时,小时和分钟零消失,但如果我重置计时器,小时和分钟零会消失。这是我的计时器的所有代码。我已经删除了诊断问题所需的所有内容:
DateTime startTime, stopTime;
TimeSpan stoppedTime;
bool reset;
private void btnStopwatchStart_Click(object sender, EventArgs e)
{
// Start timer and get starting time
if (reset)
{
reset = false;
startTime = DateTime.Now;
stoppedTime = new TimeSpan(0);
}
else
{
stoppedTime += DateTime.Now - stopTime;
}
stopwatchTimer.Enabled = true;
}
private void btnStopwatchReset_Click(object sender, EventArgs e)
{
// Reset displays to zero
reset = true;
lblElapsed.Text = "00:00:00";
}
private void btnStopwatchPause_Click(object sender, EventArgs e)
{
// Stop timer
stopTime = DateTime.Now;
stopwatchTimer.Enabled = false;
}
private void stopwatchTimer_Tick(object sender, EventArgs e)
{
DateTime currentTime;
// Determine elapsed and total times
currentTime = DateTime.Now;
// Display times
lblElapsed.Text = HMS(currentTime - startTime - stoppedTime);
}
private string HMS(TimeSpan tms)
{
// Format time as string, leaving off last six decimal places
string s = tms.ToString();
return (s.Substring(0, s.Length - 6));
}
I have a stopwatch timer that I've added to my program. It's working fine on my Win 7 machine, and on the Vista machines I've tried, but in XP, the hrs and mins zeros disappear when the timer starts, but will come back if I reset the timer. Here's all of my code that I have for the timer. I've removed everything that didn't seem necessary to diagnose the problem:
DateTime startTime, stopTime;
TimeSpan stoppedTime;
bool reset;
private void btnStopwatchStart_Click(object sender, EventArgs e)
{
// Start timer and get starting time
if (reset)
{
reset = false;
startTime = DateTime.Now;
stoppedTime = new TimeSpan(0);
}
else
{
stoppedTime += DateTime.Now - stopTime;
}
stopwatchTimer.Enabled = true;
}
private void btnStopwatchReset_Click(object sender, EventArgs e)
{
// Reset displays to zero
reset = true;
lblElapsed.Text = "00:00:00";
}
private void btnStopwatchPause_Click(object sender, EventArgs e)
{
// Stop timer
stopTime = DateTime.Now;
stopwatchTimer.Enabled = false;
}
private void stopwatchTimer_Tick(object sender, EventArgs e)
{
DateTime currentTime;
// Determine elapsed and total times
currentTime = DateTime.Now;
// Display times
lblElapsed.Text = HMS(currentTime - startTime - stoppedTime);
}
private string HMS(TimeSpan tms)
{
// Format time as string, leaving off last six decimal places
string s = tms.ToString();
return (s.Substring(0, s.Length - 6));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是旧版本的 .NET?您的 HMS() 函数主要取决于 TimeSpan.ToString() 生成的位数。这是一种更好的格式化方法:
Older version of .NET, maybe? Your HMS() function critically depends on the number of digits generated by TimeSpan.ToString(). Here's a better way to format it: