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

后端 未结 5 2129
你的背包
你的背包 2021-01-20 00:25

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

5条回答
  •  无人及你
    2021-01-20 00:35

    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."

    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;
            }
        }
    }
    

提交回复
热议问题