[solved] How to clear DateTimePicker text or value when a form is loaded or 'clear button' is clicked using c# windows?

While working with windows form calendar control, on load it shows today’s date, even after unticking the checkbox (which supposed clear date time), the text is not disappearing, so to clear the DateTimePicker control value when we click on the clear button and when the form is loaded, here is the simple solution.


There is no straightforward answer to solve the problem but using a simple trick we can clear the text-field of the DateTimePicker or when you click the ‘Clear’ button.

If we want to clear the text of the datetimepicker text when the form loads, we can simply add the below code in the class constructor method of the form’s code-behind file Form1.cs, this will clear the text when the form is loaded for the first time.

So, create a form and add a DateTImePicker control with a Clear button, you can choose any format, if you want to display it in the text-field of the DateTImePicker control like Long, Short, Time, Custom.

Highlight Datetimepicker and right-click for its properties, once you opened the date-timepicker control properties in the right-side panel, add MouseUp event like as shown in the below image. By double clicking on DateTimePicker1_MouseUp field to generate an event method in the code-behind .cs file

Do copy paste the below code in DateTimePicker1_MouseUp event method, the below code clears the dateTimePicker text and the value of the control when tick/untick the check-box in the datetimepicker checkbox.

Add a Clear button event like as shown in the above image and do double-click on Button1_Click field to generate an event method in the code-behind cs file and add the below logic.

so, the final complete Form1.cs class code looks like this.



As we discussed in this article, there is no straightforward answer to clear the text and value of the DateTimePicker control field, So by making some changes like showing the checkbox and adding the code in Mouse_Up event, we can achieve this requirement.

Shivaraju M

Over 3+ years experience in IT industry, good knowledge in . Net-based Web applications, windows applications, web services, and SPAs. I have played multiple roles related to technical delivery in multiple domains while working on over 10+ technologies. I am currently focused on continuous improvement and agile project management.

Post a Comment

Previous Post Next Post