Click here to Skip to main content
15,299,443 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After clear/Null dateTimePicker once date time not showing again means not working again
C#
private void radioButton11_CheckedChanged(object sender, EventArgs e)
{
   //Both lines are not working after clear/null dateTimePicker once          
    dateTimePicker1.Value = DateTime.Now; 
    dateTimePicker2.ResetText();
}

private void radioButton12_CheckedChanged(object sender, EventArgs e)
{
    dateTimePicker1.CustomFormat = " ";
    dateTimePicker2.CustomFormat = " ";
}


What I have tried:

C#
private void radioButton11_CheckedChanged(object sender, EventArgs e)
{
   //Both lines are not working after clear/null dateTimePicker once          
    dateTimePicker1.Value = DateTime.Now; 
    dateTimePicker2.ResetText();
}
private void radioButton12_CheckedChanged(object sender, EventArgs e)
{
    dateTimePicker1.CustomFormat = " ";
    dateTimePicker2.CustomFormat = " ";
}
Posted
Updated 26-Aug-20 16:45pm
v2

That's know/faced by few, look here[^].

This is because you are setting it to null and resets to bad format.

Following has been shared working across:
Logic is to reset date to minimum and then use ValueChanged event to hide it in display using what you already tried.

C#
private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    if (dateTimePicker1.Value == DateTimePicker.MinimumDateTime)
    {
        dateTimePicker1.Value = DateTime.Now; // This is required in order to show current month/year when user reopens the date popup.
        dateTimePicker1.Format = DateTimePickerFormat.Custom;
        dateTimePicker1.CustomFormat = " ";
    }
    else
    {
        // Set the format that you want it in. For example, Short set here. 
        dateTimePicker1.Format = DateTimePickerFormat.Short;
    }
}

private void Clear_Click(object sender, EventArgs e)
{
    dateTimePicker1.Value = DateTimePicker.MinimumDateTime;
}
   
Comments
Istehak Ahmad 26-Aug-20 22:19pm
   
After run Clear_Click it`s ok, After that how I will get the value in dateTimePicker1?
Issue is that after clear dateTimePicker1 I am not able to get date time, dateTimePicker1 is showing blank
//I got the Solution 

private void radioButton11_CheckedChanged(object sender, EventArgs e)
{         
    dateTimePicker1.Value = DateTime.Now; 
    dateTimePicker2.ResetText();

    //add dateTimePicker1.CustomFormat = "dd/MMM/yy";
    dateTimePicker1.CustomFormat = "dd/MMM/yy";
    dateTimePicker2.CustomFormat = "dd/MMM/yy";   
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900