How can I make a DateTimePicker display an empty string?

后端 未结 8 687
不知归路
不知归路 2020-11-28 08:12

I would like to be able to display a DateTimePicker that has a default value of nothing, i.e. no date.

For example, I have a start date dtTaskStar

8条回答
  •  孤独总比滥情好
    2020-11-28 09:06

    When I want to display an empty date value I do this

                if (sStrDate != "")
                {
                    dateCreated.Value = DateTime.Parse(sStrDate);
                }
                else
                {
                    dateCreated.CustomFormat = " ";
                    dateCreated.Format = DateTimePickerFormat.Custom;
                }
    

    Then when the user clicks on the control I have this:

                private void dateControl_MouseDown(object sender, MouseEventArgs e)
                {
                    ((DateTimePicker)sender).Format = DateTimePickerFormat.Long;    
                }
    

    This allows you to display and use an empty date value, but still allow the user to be able to change the date to something when they wish.

    Keep in mind that sStrDate has already been validated as a valid date string.

提交回复
热议问题