简单的倒计时闹钟,但有问题..不知道为什么! [英] Simple countdown alarm, but having problems.. no idea why!

查看:90
本文介绍了简单的倒计时闹钟,但有问题..不知道为什么!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用Windows窗体在C#中创建一个简单的闹钟..



我可以让它工作但是它有几个问题,有在倒计时开始之前稍微延迟并且面板在它达到0之前改变颜色一秒,即使代码已将其设置为在0上改变颜色。



它应该是用户工作输入你想要倒计时的文本框中的秒数,然后它会在另一个文本框中倒计时,一旦倒计时到达0,颜色面板应该从绿色变为红色,感谢任何提前帮助!



另外,我不希望用户在倒计时文本框中输入任何内容,因为它们在执行此操作时会导致错误..我应该如何预防它?



以下是我的代码:



Hi I'm creating a simple alarm in C# using windows form..

I can get it to work but it's got a couple of problems, there is a slight delay before the countdown begins and the panel changes colour one second before it hits 0, even though the code has set it to change colour on 0.

It's supposed to work by the user enters the amount of seconds in the textbox which you want to count down from, then it will countdown in another text box, once the countdown hits 0 the color panel is supposed to change from green to red, thanks for any help in advance!

Also, I don't want the user to input anything in the countdown textbox because it causes errors when they do this.. how should I prevent it?

Below is my code:

int duration;

private void btnStart_Click(object sender, EventArgs e)
{

    timer1.Enabled = true;
    timer1.Start();

    duration = int.Parse(txtCountdown.Text);
    txtTimer.Text = duration.ToString();

}

private void timer1_Tick(object sender, EventArgs e)
{

    txtTimer.Text = duration.ToString();

    duration--;

    if (duration == 0)
    {
        pnlAlarm.BackColor = Color.Red;
        timer1.Stop();

    }

}

推荐答案

你不能指望这个解决方案的准确性,因为你总结了几个短时间段。不需要计算持续时间并将其与零进行比较,您需要实时,这可以使用 System.Diagnostics.Stopwatch 以高精度完成。不要使用持续时间,输出秒表所用的时间。



另一个问题取决于类型你使用的计时器:处理程序可能需要调用,但对于 System.Windows.Forms.Timer 你不会需要关心它;但是这种计时器的准确性很差。对于闹钟,它可能已经足够了。







此外,它会很好完全不使用计时器。如果将其与秒表结合使用,则可以使用更精确的线程。你需要使用 Conrol.Invoke 来输出。



-SA
You cannot count on accuracy of this solution, because you summarize several short periods of time. Instead of counting duration and comparing it with zero, you need to could "real time", which can be done with hight accuracy using the System.Diagnostics.Stopwatch. Don't use duration at all, output the time taken from stopwatch.

Another problem depends on the type of the timer you use: the handler may require Invoke, but for System.Windows.Forms.Timer you would not need to care about it; however the accuracy of this type of timer is ve81ry bad. For alarm, it could be good enough.



Also, it would be good not using timer at all. You can use a thread with better accuracy, if you combine it with the stopwatch. You will need to use Conrol.Invoke for output.

—SA


不确定,但试试这个,

Not sure, but try this out,
private void timer1_Tick(object sender, EventArgs e)
{

    txtTimer.Text = duration.ToString();

    duration--;

    if (duration <= 0)
    {
        pnlAlarm.BackColor = Color.Red;
        timer1.Stop();
        timer1.Enabled = false;

    }

}





-KR



-KR


这篇关于简单的倒计时闹钟,但有问题..不知道为什么!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆