C # Set the DateTimePicker initial value to null

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.