[.net] How can I make a DateTimePicker display an empty string?

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 dtTaskStart and an end date dtTaskEnd for a task, but the end date is not known, and not populated initially.

I have specified a custom format of yyyy-MM-dd for both controls.

Setting the value to null, or an empty string at runtime causes an error, so how can I accomplish this?

I have considered using a checkbox to control the enabling of this field, but there is still the issue of displaying an initial value..

Edit:
Arguably a duplicate of the question DateTimePicker Null Value (.NET), but the solution I found for my problem is not a solution for that question, so I think it should remain here for others to find..

This question is related to .net winforms datetimepicker

The answer is


The basic concept is the same told by others. But its easier to implement this way when you have multiple dateTimePicker.

dateTimePicker1.Value = DateTime.Now;
dateTimePicker1.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged);
dateTimePicker1.ShowCheckBox=true;
dateTimePicker1.Checked=false;


dateTimePicker2.Value = DateTime.Now;
dateTimePicker2.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged);
dateTimePicker2.ShowCheckBox=true;
dateTimePicker2.Checked=false;

the value changed event function

        void Dtp_ValueChanged(object sender, EventArgs e)
        {
            if(((DateTimePicker)sender).ShowCheckBox==true)
            {
                if(((DateTimePicker)sender).Checked==false)
                {
                    ((DateTimePicker)sender).CustomFormat = " ";
                    ((DateTimePicker)sender).Format = DateTimePickerFormat.Custom;
                }
                else
                {
                    ((DateTimePicker)sender).Format = DateTimePickerFormat.Short;
                }
            }
            else
            {
                ((DateTimePicker)sender).Format = DateTimePickerFormat.Short;
            }
        }

When unchecked enter image description here

When checked enter image description here


Better to use text box for calling/displaying date and while saving use DateTimePicker. Make visible property true or false as per requirement.

For eg : During form load make Load date in Textbox and make DTPIcker invisible and while adding vice versa


this worked for me for c#

if (enableEndDateCheckBox.Checked == true)
{
    endDateDateTimePicker.Enabled = true;
    endDateDateTimePicker.Format = DateTimePickerFormat.Short;                
}
else
{
    endDateDateTimePicker.Enabled = false;
    endDateDateTimePicker.Format = DateTimePickerFormat.Custom;
    endDateDateTimePicker.CustomFormat = " ";
}

nice one guys!


Just set the property as follows:

When the user press "clear button" or "delete key" do

dtpData.CustomFormat = " "  'An empty SPACE
dtpData.Format = DateTimePickerFormat.Custom

On DateTimePicker1_ValueChanged event do

dtpData.CustomFormat = "dd/MM/yyyy hh:mm:ss"

In case anybody has an issue with setting datetimepicker control to blank during the form load event, and then show the current date as needed, here is an example:

MAKE SURE THAT CustomFormat = " " has same number of spaces (at least one space) in both methods

Private Sub setDateTimePickerBlank(ByVal dateTimePicker As DateTimePicker)    

    dateTimePicker.Visible = True
    dateTimePicker.Format = DateTimePickerFormat.Custom
    dateTimePicker.CustomFormat = "    " 
End Sub


Private Sub dateTimePicker_MouseHover(ByVal sender As Object, ByVal e As 
                    System.EventArgs) Handles dateTimePicker.MouseHover

    Dim dateTimePicker As DateTimePicker = CType(sender, DateTimePicker)
    If dateTimePicker.Text = "    " Then
        dateTimePicker.Text = Format(DateTime.Now, "MM/dd/yyyy")
    End If
End Sub

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.


Listen , Make Following changes in your code if you want to show empty datetimepicker and get null when no date is selected by user, else save date.

  1. Set datetimepicker FORMAT property to custom.
  2. set CUSTOM FORMAT property to empty string " ".
  3. set its TAG to 0 by default.
  4. Register Event for datetimepicker VALUECHANGED.

Now real work starts

if user will interact with datetimepicker its VALUECHANGED event will be called and there set its TAG property to 1.

Final Step

Now when saving, check if its TAG is zero, then save NULL date else if TAG is 1 then pick and save Datetime picker value.

It Works like a charm.

Now if you want its value be changed back to empty by user interaction, then add checkbox and show text "Clear" with this checkbox. if user wants to clear date, simply again set its CUSTOM FORMAT property to empty string " ", and set its TAG back to 0. Thats it..


Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to datetimepicker

Angular-Material DateTime Picker Component? DateTimePicker time picker in 24 hour but displaying in 12hr? Set default format of datetimepicker as dd-MM-yyyy Set default time in bootstrap-datetimepicker Get the value of bootstrap Datetimepicker in JavaScript Bootstrap date and time picker datetimepicker is not a function jquery How to change the date format of a DateTimePicker in vb.net jQuery date/time picker How to get only the date value from a Windows Forms DateTimePicker control?