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?
Thanks a lot.
By default, the DatePicker value is null and the Calendar popup is hidden.
The Windows Forms DateTimePicker control allows the user to select a single item from a list of dates or times. When used to represent a date, it appears in two parts: a drop-down list with a date represented in text, and a grid that appears when you click on the down-arrow next to the list.
The DateTimePicker control allows you to display and collect date and time from the user with a specified format. The DateTimePicker control has two parts, a label that displays the selected date and a popup calendar that allows users to select a new date.
You can't have a valueless datepicker with the out-of-the-box control. Why? It is backed by DateTime, which is non-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.
ValueChanged
event, set the flag's value to falseValueChanged
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.
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.
Have a look here for a nullable datetimepicker on CodeProject, in fact there are a few here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With