CountDown检查时间

时间:2014-04-13 16:33:50

标签: c# visual-studio-2012 timer count countdown

我做了一个倒计时,我想现在为它添加时间检查。如果分钟是< 01秒,秒数为!= 60所以00:59时间应为橙色,如果秒数小于10则时间应为红色。

但它不起作用。 如果时间是00:00:58,他们总是变得橙色,但为什么呢?

private int hours, minutes, seconds;
private bool paused; 

private void button_Start_Click(object sender, EventArgs e)
{

    button_Pause.Enabled = true;
    button_Stop.Enabled = true;

    if(paused != true)
    {
        hours = int.Parse(textBox_Hours.Text);
        minutes = int.Parse(textBox_Minutes.Text);
        seconds = int.Parse(textBox_Seconds.Text) + 1;
        textBox_Hours.Enabled = false;
        textBox_Minutes.Enabled = false;
        textBox_Seconds.Enabled = false;
        button_Start.Enabled = false;
        timer_CountDown.Start();

    }
}


private void timer_CountDown_Tick(object sender, EventArgs e)
{
    if(hours == 0 && minutes < 1)
    {
        label_Hours.ForeColor = Color.Red;
        label_Minutes.ForeColor = Color.Red;
        label_Seconds.ForeColor = Color.Red;
        label8.ForeColor = Color.Red;
        label10.ForeColor = Color.Red;
    }

    if(hours == 0 && minutes == 0 && seconds == 0)
    {
        timer_CountDown.Stop();
        textBox_Seconds.Enabled = true;
        textBox_Minutes.Enabled = true;
        textBox_Hours.Enabled = true;
        button_Start.Enabled = true;
    }
    else
    {
        if (seconds < 1)
        {
            seconds = 59;
            if (minutes < 1)
            {
                minutes = 59;
                if (hours != 0)
                {
                    hours -= 1;
                }
            }
            else
            {
                minutes -= 1;
            }
        }
        else
        {
            seconds -= 1;
        }

        if(hours > 9)
        {
            label_Hours.Text = hours.ToString();
        }
        else { label_Hours.Text = "0" + hours.ToString(); }

        if(minutes > 9) 
        {
            label_Minutes.Text = minutes.ToString();
        }
        else { label_Minutes.Text = "0" + minutes.ToString(); }

        if(seconds > 9)
        {
            label_Seconds.Text = seconds.ToString();
        }
        else { label_Seconds.Text = "0" + seconds.ToString(); }
    }
}

Timer Intervall是1000。

1 个答案:

答案 0 :(得分:1)

你过度复杂化了。为什么不使用TimeSpan类型并删除那些hours, minutes, seconds

private TimeSpan countDownTime = TimeSpan.Zero;

private void timer_CountDown_Tick(object sender, EventArgs e)
{
    if(countDownTime == TimeSpan.Zero)
    {
        timer_CountDown.Stop();
        textBox_Seconds.Enabled = true;
        textBox_Minutes.Enabled = true;
        textBox_Hours.Enabled = true;
        button_Start.Enabled = true;
        return;
    }

    countDownTime = countDownTime.Add(TimeSpan.FromSeconds(1).Negate());

    label_Hours.Text = countDownTime.ToString("hh");
    label_Minutes.Text = countDownTime.ToString("mm");
    label_Seconds.Text = countDownTime.ToString("ss");

    if(countDownTime.TotalSeconds < 10)
    {
        label_Hours.ForeColor = Color.Red;
        label_Minutes.ForeColor = Color.Red;
        label_Seconds.ForeColor = Color.Red;
        label8.ForeColor = Color.Red;
        label10.ForeColor = Color.Red;
    }
    else if (countDownTime.TotalMinutes < 1)
    {
        label_Hours.ForeColor = Color.Orange;
        label_Minutes.ForeColor = Color.Orange;
        label_Seconds.ForeColor = Color.Orange;
        label8.ForeColor = Color.Orange;
        label10.ForeColor = Color.Orange;
    }
}

private void button_Start_Click(object sender, EventArgs e)
{
    button_Pause.Enabled = true;
    button_Stop.Enabled = true;

    if(paused != true)
    {
        int hours = int.Parse(textBox_Hours.Text);
        int minutes = int.Parse(textBox_Minutes.Text);
        int seconds = int.Parse(textBox_Seconds.Text) + 1;

        this.countDownTime = new TimeSpan(hours,minutes,seconds);         

        textBox_Hours.Enabled = false;
        textBox_Minutes.Enabled = false;
        textBox_Seconds.Enabled = false;
        button_Start.Enabled = false;
        timer_CountDown.Start();
    }
}