简体   繁体   English

如何使时间计数器从当前点而不是从开始继续向上/向下?

[英]How can I make the time counter to continue up/down from the current point and not from the start?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Extract
{
    public partial class TimeCounter : Label
    {
        public bool CountUp { get; set; }

        private Timer _timer;
        private int _elapsedSeconds;
        private TimeSpan ts = TimeSpan.FromSeconds(100);

        public TimeCounter()
        {
            InitializeComponent();

            StartCountDownTimer();
        }

        public void StartCountDownTimer()
        {
            _timer = new Timer
            {
                Interval = 1000,
                Enabled = true
            };
            _timer.Tick += (sender, args) =>
            {
                if (CountUp == false)
                {
                    ts = ts.Subtract(TimeSpan.FromSeconds(1));
                    this.Text = ts.ToString();
                }
                else
                {
                    _elapsedSeconds++;
                    TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
                    this.Text = time.ToString(@"hh\:mm\:ss");
                }
            };
        }

        private void TimeCounter_Load(object sender, EventArgs e)
        {

        }
    }
}

And in form1在form1中

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked)
            {
                timeCounter1.CountUp = true;
            }
            else
            {
                timeCounter1.CountUp = false;
            }
        }

When I change the CountUp flag in form1 it's changing the time counter direction up/down but it's starting over each time.当我在 form1 中更改 CountUp 标志时,它会向上/向下更改时间计数器方向,但每次都会重新开始。 Id it's counting up then it start from 00:00:00 and if counting down then from 1 minutes and 40 seconds 00:01:40 Id 它正在计数,然后从 00:00:00 开始,如果倒计时,则从 1 分 40 秒 00:01:40

How can I make that when I change the flag it will change the direction from the current time and not from the start ?当我更改标志时,我怎样才能做到这一点,它会从当前时间而不是从一开始就改变方向?

If the time is for example 00:00:13 (counting up) and I change the flag to count down then count down from 00:00:13 ... 00:00:12... same the other way if it's counting down and I change it to count up then continue up from the current time.例如,如果时间是 00:00:13(向上计数)并且我将标志更改为向下计数,则从 00:00:13 ... 00:00:12 向下计数...如果正在计数,则以另一种方式进行计数向下,我将其更改为向上计数,然后从当前时间继续向上。

What do you need "_elapsedSeconds" for?你需要“_elapsedSeconds”做什么?

Just use?就用?

_timer.Tick += (sender, args) =>
{
    if (CountUp)
    {
        ts = ts.Add(TimeSpan.FromSeconds(1));        
    }
    else
    {
        ts = ts.Subtract(TimeSpan.FromSeconds(1));            
    }
    this.Text = ts.ToString();
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用标志简化脚本? 以及如何使淡入/淡出从当前点继续到中断方向? - How can I simplify the script using the flags? and how can I make the fade in/out continue from the current point to the interrupted direction? 如何创建一个倒计时到给定时间和日期的计数器? - How can I create a counter that counts down to a given time and date? 如何将当前执行状态推送到堆栈中以便以后可以继续执行? - How can I push the current execution state into a stack so that I can continue from it later? 我怎样才能让播放器从我暂停的地方继续漂浮? - How can I make the player continue floating from the spot where I paused it from? 我如何使对象将不停地自动向上/向下缩放? - How can I make that the object will scale automatic up/down nonstop? 如何让X轴从0开始并每秒更新而不是使用当前时间(DateTime)? - How do i get X Axis to start from 0 and update each second instead of using the current time(DateTime)? 如何多次快速按F可以平滑地缩放对象而无需等待? - How can I make that when pressing F fast many time it will scale the object down/up smooth without waiting? 如何根据当前日期时间延迟计时器的运行或启动 - How to delay a timer from running or start it based on current date time 我如何从特定点开始阅读文本文件? - How do i start reading a text file from a specific point? 如何在执行期间暂停、保存状态并稍后从同一点继续? - How do I pause during execution, save state, and continue from same point later on?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM