显示时间跨度的标签在 XP 中消失,但在较新的 Windows 版本中不会消失

发布于 2024-09-04 02:55:07 字数 1477 浏览 4 评论 0原文

我有一个秒表计时器,已添加到我的程序中。它在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-09-11 02:55:07

也许是旧版本的 .NET?您的 HMS() 函数主要取决于 TimeSpan.ToString() 生成的位数。这是一种更好的格式化方法:

    private static string HMS(TimeSpan tms) {
        return new DateTime(tms.Ticks).ToString("H:mm:ss");
    }

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");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文