如果日期时间选择器中的时间是eqaul到datetime.now,我该如何开始操作? [英] How can I start an action if the time in the datetime picker is eqaul to datetime.now?

查看:123
本文介绍了如果日期时间选择器中的时间是eqaul到datetime.now,我该如何开始操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我是这个论坛的新手,这是我的第一篇文章。我来自德国,差不多17岁。



我的问题有点广泛。

我有一个正常的WindowsFormsApplication。

有7个DateTimePickers,用户可以设置7个不同的时间(hh:mm:ss)。

在程序的底部,有一个应该显示的标签实际时间与7个用户定义时间之一的最小差异。

如果差异等于00:00:00,程序应该显示一个MessageBox,带有差异的标签应该从00更新:00:00到新的差异,实际时间与用户定义的6个时间之一之间的第二个最小差异。

我一周7天都需要这个功能。





我希望,你理解。如果您有任何问题或建议的解决方案,请写信给我。



感谢您的努力。

解决方案

我试图测试每个DateTimePicker。但我想只显示标签中的较小差异。所以我在计时器中写了很多if-querys来显示最小的差异。

如何编写7个DateTimePickers的if-querys?




不要! :笑:方式,方式太多组合。

所以...设置一个TimeSpan值数组,并加载7 DateTimePicker值减去当前dateTime:

 DateTime now = DateTime.Now; 
Timespan [] diffs = new TimeSpan [ 7 ];
diffs [ 0 ] = dateTimePicker1.Value - now;
diffs [ 1 ] = dateTimePicker2.Value - now;
diffs [ 2 ] = dateTimePicker3.Value - now;
diffs [ 3 ] = dateTimePicker4.Value - now;
diffs [ 4 ] = dateTimePicker5.Value - now;
diffs [ 5 ] = dateTimePicker6.Value - now;
diffs [ 6 ] = dateTimePicker7.Value - now;



然后使用Array.Sort和你可以轻松获得最大和最小值。

 Array.Sort(diffs); 
Timespan min = diffs [ 0 ];
Timespan max = diffs [ 6 ];

(显然,我不会在现实世界中使用魔术数字)



并且只能在一个方法中读取一次:它在你的控制之外变化,所以即使这样做:

DateTime now1 = DateTime 。现在;

DateTime now2 = DateTime.Now;

  if ( now1!= now2) throw   new  ApplicationException( 时间改变了!); 

有时候,你会得到一个例外,有时你不会......而且他们可以看起来非常不同 - 如果你在错误的时间做这个千禧年! :笑:


Hey guys, I'm new in this forum and this is my first post. I come from Germany and I'm almost 17 years old.

My problem is a little extensive.
I hava a normal WindowsFormsApplication.
There are 7 DateTimePickers where the user can set 7 different times (hh:mm:ss).
At the bottom of my program, there is a Label which should show the smallest difference between the actual time and one of the 7 user defined times.
If the difference equals 00:00:00 the program should show me a MessageBox and the label with the difference should update from 00:00:00 to the new difference with the 2nd smallest difference between the actual time and one of the 6 left user defined times.
This function I need for all 7 days of the week.


I hope, you understood. If you have any questions or a suggested solution, please write me.

Thanks for efforts.

解决方案

"I tried to test for every DateTimePicker. But I want to show only the smalles difference in the label. So I wrote a lot of if-querys in a timer to show only the smallest difference.
How can I write the if-querys for 7 DateTimePickers?"


Don't! :laugh: way, way too many combinations.
So...set up an array of TimeSpan values, and load it with the 7 DateTimePicker values minus the current dateTime:

DateTime now = DateTime.Now;
Timespan[] diffs = new TimeSpan[7];
diffs[0] = dateTimePicker1.Value - now;
diffs[1] = dateTimePicker2.Value - now;
diffs[2] = dateTimePicker3.Value - now;
diffs[3] = dateTimePicker4.Value - now;
diffs[4] = dateTimePicker5.Value - now;
diffs[5] = dateTimePicker6.Value - now;
diffs[6] = dateTimePicker7.Value - now;


Then use Array.Sort and you have the maximum and minimum easily available.

Array.Sort(diffs);
Timespan min = diffs[0];
Timespan max = diffs[6];

(Obviously, I wouldn't use "magic numbers" in the real world)

And only ever read the time once in a method: it changes outside your control, so even doing this:
DateTime now1 = DateTime.Now;
DateTime now2 = DateTime.Now;

if (now1 != now2) throw new ApplicationException("The time changed!");

Sometimes, you will get an exception, and sometimes you won't...and they can seem very different - different millenium if you do it at the wrong time! :laugh:


这篇关于如果日期时间选择器中的时间是eqaul到datetime.now,我该如何开始操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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