简体   繁体   English

如何将 win 表单日期时间选择器设置为没有默认值?

[英]How to set win forms datetime picker to have no default value?

I am implementing search functionality in WinForms and I search by date range.我正在 WinForms 中实现搜索功能,并按日期范围进行搜索。 Thus there are dateForm and dateTo date pickers on the form.因此,表单上有 dateForm 和 dateTo 日期选择器。 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.默认情况下,它们的值是日期时间 now(),如果用户根本不触摸日期时间选择器,他将不会得到任何结果。 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()因为搜索将在 now() 和 now() 之间执行,如果我将最小值和最大值设为默认值,它会解决第一个问题,但如果用户想按日期范围搜索,则会出现另一个问题,他需要单击许多从默认 1700(某物)到现在()的时间

Any suggestions to solve this problem?有什么建议可以解决这个问题吗?

Thanks a lot.非常感谢。

看看这里的CodeProject上可为空的DateTimePicker,其实有几个在这里

You can't have a valueless datepicker with the out-of-the-box control.您不能拥有带有开箱即用控件的无价值日期选择器。 Why?为什么? It is backed by DateTime, which is non-nullable.它由不可为空的 DateTime 支持。

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

Edit:编辑:

In response to your comment, yes, you can do this;回应您的评论,是的,您可以这样做; in fact, I've done it.事实上,我已经做到了。

  • 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使用字段或私有属性来保存 'from' 和 'to' 日期,而不是从 dtp 读取它们,并将它们的默认值设置为 min 和 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使用布尔标志来指示何时在代码中操作 dtp 值,并在 dtp 的ValueChanged事件中,将标志的值设置为 false
  • in the form load event, set the flag to true and dtp value to today's date在表单加载事件中,将标志设置为 true 并将 dtp 值设置为今天的日期
  • 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).同样在ValueChanged事件中,将fromto字段设置为 dtps 的值(当任一 dtp 更改时,您必须同时设置这两个字段,因为用户会看到另一个设置为今天,但搜索值仍然是 min或最大)。

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.此外,用户不能选择“仅今天”而不先更改日期之一,然后再将其更改回来。

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.我认为对您来说最好的解决方案是设置一个复选框,“按日期范围搜索”,它要么启用两个被禁用的 dtps,要么显示隐藏的 dtps。 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.然后,你从最小搜索到最大,除非该复选框被选中,当复选框选中,您可以使用这两个DTP日期,不管它们是什么。 Don't forget to deal with to and from being out of order, which can be done in several ways.不要忘记处理to and from乱序,这可以通过多种方式完成。

Put a check box next to each datetime picker, and use the check box to enable/disable the datetime picker.在每个日期时间选择器旁边放置一个复选框,并使用该复选框启用/禁用日期时间选择器。

So if the datetimepicker is disabled, you know the user do not want to specify the datetime.因此,如果 datetimepicker 被禁用,您就知道用户不想指定日期时间。

You can set DateDateTimePicker.Format property to Custom .您可以将DateDateTimePicker.Format属性设置为Custom

Then set DateDateTimePicker.CustomFormat property to your default text (eg "N/A" or a space " " )然后将DateDateTimePicker.CustomFormat属性设置为您的默认文本(例如"N/A"或空格" "

After the user has selected a certain date value, you should set back the format property to short etc.在用户选择了某个日期值后,您应该将 format 属性设置为 short 等。

例子

Hope this helps!希望这可以帮助!

The DateTimePicker has a ShowCheckBox property that "Determines whether a check box is displayed in the control. When the box is unchecked, no value is selected." DateTimePicker 具有 ShowCheckBox 属性,该属性“确定控件中是否显示复选框。取消选中该框时,不会选择任何值。”

I've used something like the following for date range selectors that can be empty.对于可以为空的日期范围选择器,我使用了类似以下的内容。 One user told me that at first, she didn't know what the check box was for, but she figured it out on her own after using it a couple of times.一位用户告诉我,一开始她不知道这个复选框是干什么用的,但她用了几次后就自己弄明白了。

public DateTime? EndDate
{
    get
    {
        DateTime? returnValue = null;
        if (endDateDateTimePicker.Checked) 
        { 
            returnValue = endDateDateTimePicker.Value; 
        }
        return returnValue;
    }
    set
    {
        if (value.HasValue)
        {
            endDateDateTimePicker.Checked = true;
            endDateDateTimePicker.Value = value.Value;
        }
        else
        {
            endDateDateTimePicker.Checked = false;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM