如何将 win 表单日期时间选择器设置为没有默认值? [英] How to set win forms datetime picker to have no default value?

查看:28
本文介绍了如何将 win 表单日期时间选择器设置为没有默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 WinForms 中实现搜索功能,并按日期范围进行搜索.因此,表单上有 dateForm 和 dateTo 日期选择器.默认情况下,它们的值是日期时间 now(),如果用户根本不触摸日期时间选择器,他将不会得到任何结果.因为搜索将在 now() 和 now() 之间执行,如果我将最小值和最大值设为默认值,它将解决第一个问题,但如果用户想按日期范围搜索,则会出现另一个问题,他需要单击许多从默认 1700(某物)到现在()的时间

I am implementing search functionality in WinForms and I search by date range. Thus there are dateForm and dateTo date pickers on the form. By default their values are date time now() and if user do not touch date time pickers at all he will not get any results. Because search will be performed between now() and now(), also if I put min and max values as default it would solve first problem but there would be another problem if user wants to search by date range, he would need to click many times to come from default 1700 (something) to now()

有什么解决这个问题的建议吗?

Any suggestions to solve this problem?

非常感谢.

推荐答案

您不能拥有带有开箱即用控件的毫无价值的日期选择器.为什么?它由不可为空的 DateTime 支持.

You can't have a valueless datepicker with the out-of-the-box control. Why? It is backed by DateTime, which is non-nullable.

您可以使用另一个控件禁用它,或者在用户单击之前禁用它(对于像我这样的键盘爱好者来说糟糕的 UX),或者找到或创建(!)一个使用 Nullable.

You can disable it with another control, or leave it disabled until the user clicks (bad UX for keyboard enthusiasts, like myself), or find or create (!) one that uses Nullable<DateTime>.

针对您的评论,是的,您可以这样做;事实上,我已经做到了.

In response to your comment, yes, you can do this; in fact, I've done it.

  • 使用字段或私有属性来保存from"和to"日期,而不是从 dtp 中读取它们,并将它们的默认值设置为 min 和 max
  • 使用布尔标志来指示您何时在代码中操作 dtp 值,并在 dtp 的 ValueChanged 事件中,将标志的值设置为 false
  • 在表单加载事件中,将标志设置为 true 并将 dtp 值设置为今天的日期
  • 也在 ValueChanged 事件中,将 fromto 字段设置为 dtps 的值(当dtp 更改,因为用户会看到另一个设置为今天,但搜索值仍将是 min 或 max).
  • use fields or private properties to hold the 'from' and 'to' dates, instead of reading them from the dtp, and set their defaults to min and max
  • use a boolean flag to indicate when you are manipulating the dtp value in code, and in the dtp's ValueChanged event, set the flag's value to false
  • in the form load event, set the flag to true and dtp value to today's date
  • also in the ValueChanged event, set the from and to fields to the values of the dtps (you have to set both when either dtp changes, because the user will see the other one as set to today, but the search value will still be min or max).

这样做的问题是,一旦用户更改了日期选择,她就无法轻松返回所有日期".此外,用户不能选择仅限今天".无需先更改其中一个日期,然后再将其更改回来.

The problems with this is that once the user has changed the date selection, she can't easily go back to "all dates." Furthermore, the user can't select "today only" without first changing one of the dates and then changing it back.

我认为对您来说最好的解决方案是设置一个按日期范围搜索"复选框.它要么启用两个被禁用的 dtps,要么显示否则隐藏的 dtps.然后你从最小值到最大值搜索,除非复选框被选中,当复选框选中时,你使用两个 dtp 日期,无论它们是什么.不要忘记处理 tofrom 乱序的情况,这可以通过多种方式完成.

I think the best solution for you is to have a checkbox, "search by date range," which either enables the two dtps that are otherwise disabled, or displays the dtps that are otherwise hidden. Then you search from min to max unless the checkbox is checked, and when the checkbox is checked, you use the two dtp dates no matter what they are. Don't forget to deal with to and from being out of order, which can be done in several ways.

这篇关于如何将 win 表单日期时间选择器设置为没有默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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