C# countdown timer

后端 未结 4 1172
有刺的猬
有刺的猬 2020-12-16 01:39

I\'m trying to make a countdown using C# and show the time in format:

hour:minutes:seconds

I\'ve tried this:

 var minutes          


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 02:11

    I would use a timer something like this. First a couple of instance variables.

    private int _countDown = 30; // Seconds
    private Timer _timer;
    

    and in the constructor or load event

    _timer = new Timer();
    _timer.Tick += new EventHandler(timer_Tick);
    _timer.Interval = 1000;
    _timer.Start();
    

    and then finally the event handler

    void timer_Tick(object sender, EventArgs e)
    {
        _countDown--;
        if (_countDown < 1)
        {
            _countDown = 30;
        }
        lblCountDown.Text = _countDown.ToString();
    }
    

提交回复
热议问题