Friday, March 27, 2009

Setting the DateTimePicker to a Blank Value

I wanted to use the DateTimePicker control on a data entry form; however, it always has to be set to a value. I did not want to set the date value to Now or some other preset value because it is not clear to the user that the value needs to be set. I found several solutions to this problem, but the one below appears to work and is easy to use.

Initialize the DateTimePicker control to a blank value by setting the Format property to Custom and the CustomFormat property to a space (" "):

DateTimePickerConstDate.CustomFormat = " "
DateTimePickerConstDate.Format = DateTimePickerFormat.Custom


Then, when the value changes, change the Format property back to its original value (or define a usable Custom Format:

Private Sub DateTimePickerConstDate_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePickerConstDate.ValueChanged

DateTimePickerConstDate.Format = DateTimePickerFormat.Short

End Sub


For more information on this solution and other approaches, see the following:

http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/46b2a370-72ec-485c-9361-bfa505bb6863/

18 comments:

rockNme2349 said...

Thanks, I've been looking for a way to do this, this is the first reasonable method I've been able to find.

Anonymous said...

Awesome

Anonymous said...

Tkx, tkx, tkx and si simple !

Anonymous said...

Thanks for the tip, it came in handy. I added a bit more to it (giving the user the ability to use delete/backspace to null out the value after setting an initial value)

http://nodedangles.wordpress.com/2010/07/13/datetimepicker-null-value/

Anonymous said...

Somehow the calenderbox doesnt close when i pick the first date. Any solutions?

Jack Kokah said...

Srego, you are the best.
Thanks really it worked like a charm.

BreedPuppetsToFight said...

This has made my day

Thanks

Mahendra Barai said...

Thanks, It is a simple solution to a complex problem.

Please share your knowledge with others like this.

God bless you.

Anonymous said...

The screen looks blank, exactly what I was trying to achieve, however the datetimepicker still has a value. I want to check if the value has been set, but it always returns the current date/time. I have tried setting the value to the minimum date value, then doing a check to see if it's the min date, then show blank. This works, but when the user goes to enter a 'proper' date, the picker starts off with a date in 1753 so the user has to scroll through 250 years before getting to a reasonable date.
So, in short, how do you set the value of the datetimepicker to blank?

hema said...

thanks..it works for me..appreciate you.

skcalicut said...

ok if you want to take default value as null to table you check its format. for blank custom format it is 8 and short it is 2.

Anonymous said...

Very helpful, thanks a lot

adetolah said...

This is so awesome. Many thanks.

adetolah said...

This is so awesome, it helped me a great deal. Many thanks.

deva said...

good one

Mark said...

This works great. The only issue I see is that after the date value is used and is blanked out you cannot select the exact same date again. Did anyone else have this issue and resolve it? If you did, can you post your solution. - Thanks in advance

Jaf said...

Thats Awesome !!! That solution worked perfectly.
Many many...Thanks

Mini said...

Perfect!