recently in a small project, there is a function is to query the data according to user selection criteria, the default value of the time control is empty, only when the user modifies the time, based on the time query. Simply put, by default or by clicking the empty button, the value of the time control DateTimePicker is empty, and the corresponding time is displayed when the event valuechanged is triggered.
At first, I thought the default value of the change control was empty, but I failed. DateTimePicker null values are not allowed. After trying many times I started looking for information on the Internet and found the following methods:
To initialize the form or place a clear button, click the Empty button to execute the following code:
C # code:
This. Datetimepicker1.format = Datetimepickerformat.custom;this. Datetimepicker2.customformat = "";
then execute the following code in the event dtp_firsttime_valuechanged of the D Atetimepicker control:
C # code:
This. Datetimepicker1.format = Datetimepickerformat.long; This. Datetimepicker1.customformat = null;
doing so can really make the controlDateTimePicker1Has an empty initial value, but there is aBug, that is, the first time you choose, if you choose exactly the default time, you will not be able to trigger the eventdtp_firsttime_valuechanged, i.e.DateTimePicker1value is still empty. This is a tricky question, so look into a lot of data and continue to study it.DateTimePicker, a property was found:Checked--WhenShowselectboxto betrue, determine whether a check box is selected to indicate that a value has been selected by the user. So I changed the properties in the code of the form initialization or click the empty buttonCheckedvalue, add the following code:
C # code:
This.dtp_firsttime. Checked = false;
this Bug was solved.
Source: http://1454396751.iteye.com/blog/2276801
C # Set the DateTimePicker initial value to null