如何设定一个安排时间 [英] How to make a seting time tick

查看:71
本文介绍了如何设定一个安排时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i我真的很新,真的需要 实例回答。

i我正在努力一个关于一个简单时钟的应用程序(实际上是一个硬件,接下来的4个小时),它有设置时间和日期按钮,闹钟,秒表,提醒。



单击设置时间和日期按钮后,将打开另一个表单并获取数据并将其传输到表单1中的标签。直到这里一切都很好,但 问题是用户设置的新时间不会勾选所有内容被冻结。

这里是我的代码



  //   in form1  

private void button1_Click( object sender,EventArgs e)
{
ClassAll obj = new ClassAll();
Form2 f2 = new Form2();
f2.ShowDialog();

label1.Text = obj.SetTimeHour(f2.Hour);
label2.Text = obj.SetTimeMin(f2.Min);
label3.Text = obj.SetTimeSecond(f2.Sec);
timer1.start();
}

// timer1

{
// 必须在这里的东西我不知道
// 保持时间不变的东西
// 并且检查没有超过限制
}


// ClassAll
public class ClassAll

public string SetTimeSecond( int s)
{
return string .Format( {0:D2} ,((s > 0 && s < 60 )? s: 0 ));
}

// =========== ==================================================
public string SetTimeMin( int min)
{
return string .Format(< span class =code-string> {0:D2},((min > 0 && min < 60 )?min: 0 ));
}

// =========== ==================================================
public string SetTimeHour( int h)
{
return string .Format(< span class =code-string> {0:D2},((h > 0 && h < 24 )?h: 0 ));
}



谢谢大家

解决方案

首先,不要创建新的计时器和不要启动/停止现有的。如果您打算使用计时器,然后将其设置为合适的时间间隔(可能是一秒钟)并保持运行。

然后,在Tick事件中将闹钟时间与当前时间进行比较:

 DateTime now = DateTime.Now; 
...
if (alarmTime > = now)
{
// 清除警报,并向用户表明
...
}

您可以保留一个警报列表,并使用单个计时器进行处理。



如果你想要要更新标签以显示当前时间,您只需将相应的代码添加到相同的Tick处理程序:

 label1.Text = now.ToString(  hh); 
label2.Text = now.ToString( mm);
label3.Text = now.ToString( ss);


Hi all,
i am really new in this and really need instance answer.
i am working on a application about a simple clock(which is actually a H.W and due for next 4 hours),it has setting time&Date button, Alarm, Stopwatch, Reminder.

after clicking the Set time&Date button another form will be open and get the data and transfer it to a label in form one. till here all is good but the problem is that the new time that user had set will not tick everything is frozen.
here is my code

//in form1

private void button1_Click(object sender, EventArgs e)
        {
            ClassAll obj = new ClassAll();
            Form2 f2 = new Form2();
            f2.ShowDialog();

            label1.Text = obj.SetTimeHour(f2.Hour);
            label2.Text = obj.SetTimeMin(f2.Min);
            label3.Text = obj.SetTimeSecond(f2.Sec);
            timer1.start();
        }

//timer1

{
//something must be here that i don't know
//something that keeps the time ticking
//and checks that doesn't over flow the limits
}


// ClassAll
public class ClassAll

public string SetTimeSecond(int s)
        {
            return string.Format("{0:D2}", ((s > 0 && s < 60) ? s : 0) );
        }

        //=============================================================
        public string SetTimeMin(int min)
        {
            return string.Format("{0:D2}", ((min > 0 && min < 60) ? min : 0));
        }

        //=============================================================
        public string SetTimeHour(int h)
        {
            return string.Format("{0:D2}", ((h > 0 && h < 24) ? h : 0) );
        }


thank you all

解决方案

First off, don't create new timers and don't start / stop the existing one. If you are going to use a timer then set it for a suitable interval (a second perhaps) and leave it running.
Then, in the Tick event compare the Alarm time against teh current time:

DateTime now = DateTime.Now;
...
if (alarmTime >= now)
   {
   // Clear alarm, and indicate to user
   ...
   }

You can keep a list of alarms, and process them all with a single timer that way.

If you want to update a label to show the current time, you just add the appropriate code to teh same Tick handler:

label1.Text = now.ToString("hh");
label2.Text = now.ToString("mm");
label3.Text = now.ToString("ss");


这篇关于如何设定一个安排时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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