在Windows中,显示时间跨度的标签消失了,但在Windows的新版本中却没有 [英] Label displaying Timespan disappearing in XP but not in newer Windows versions
问题描述
我有一个秒表计时器,已将其添加到程序中.在我的Win 7机器和我尝试过的Vista机器上,它都可以正常工作,但是在XP中,计时器启动时,小时和分钟的零值消失了,但是如果重置计时器,它将恢复.这是计时器的所有代码.我已经删除了诊断问题似乎不需要的所有内容:
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));
}
推荐答案
也许是旧版本的.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:
private static string HMS(TimeSpan tms) {
return new DateTime(tms.Ticks).ToString("H:mm:ss");
}
这篇关于在Windows中,显示时间跨度的标签消失了,但在Windows的新版本中却没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!