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

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.

public Form1()
{
            InitializeComponent()
            // clearing the current-date in form-loading- First time
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = " ";
            dateTimePicker1.ShowCheckBox = true;
            dateTimePicker1.Checked = false;
}

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.

private void DateTimePicker1_MouseUp(object sender, MouseEventArgs e)
        {
            // clear and show the current date using check/uncheck event of calendar check box
            if (!dateTimePicker1.Checked)
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.CustomFormat = " ";
            }
            else
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.CustomFormat = "MM-dd-yyyy";
            }
        }

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.

private void Button1_Click(object sender, EventArgs e)
 {
            dateTimePicker1.Text = string.Empty;
            dateTimePicker1.Checked = false;
            dateTimePicker1.CustomFormat = " ";
            MessageBox.Show("cleared the dateTimePicker's Text" + dateTimePicker1.Text);
 }

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // clearing the current-date in form-loading- First time
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = " ";
            dateTimePicker1.ShowCheckBox = true;
            dateTimePicker1.Checked = false;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Text = string.Empty;
            dateTimePicker1.Checked = false;
            dateTimePicker1.CustomFormat = " ";
            MessageBox.Show("cleared the dateTimePicker's Text" + dateTimePicker1.Text);
        }

        private void DateTimePicker1_MouseUp(object sender, MouseEventArgs e)
        {
            // clear and show the current date using check/uncheck event of calendar check box
            if (!dateTimePicker1.Checked)
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.CustomFormat = " ";
            }
            else
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.CustomFormat = "MM-dd-yyyy";
            }
        }
    }
}

Conclusion

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.

Default image
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.

Leave a Reply