Click here to Skip to main content
13,140,231 members (49,890 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 17 Feb 2008

Nullable Compact Framework DateTimePicker Custom Control

, 17 Feb 2008
Rate this:
Please Sign up or sign in to vote.
A custom DateTimePicker control for the Compact Framework, which handles DBNull values.


The standard DateTimePicker control does not handle DBNull values, as is often desired when presenting database columns. It always displays a value, so it can appear to have been filled in by a user when in fact it has not. This is especially an issue for medical devices, which are prohibited from pre-filling data for a user. An attempt to derive a control which defaults to DBNull.Value can result in a NotSupportedException when the containing form loads.

This control, for the Compact Framework, implements a Facade Pattern to enclose a DateTimePicker inside a custom control to deal with DBNull values, and make the enclosed DateTimePicker invisible when the control has a null value, so the control looks like an empty text box. When the control is set to a non-null value, the DataTimePicker becomes visible and allows the user to interact with it. When the user clicks on the blank (nulled) control, it responds by loading DateTime.Now and making the control visible.


There have been various attempts to work around the limitations of the DateTimePicker, but other solutions do not work well in the Compact Framework because of the reduced features of the DateTimePicker. This is a fairly simple solution which satisfies the need to appear empty when no value has been explicitly entered, defaults to a null value without throwing a NotSupportedException on form load, presents to the user a familiar DateTimePicker interaction, and provides a familiar design-time experience to the developer.

Using the code

The code is used almost exactly like that for the native DateTimePicker. Once the ControlLibrary has been compiled and added to the toolbox, the CFDateTimePicker can be drag-dropped onto the form design surface. Many of the common properties of the DateTimePicker are exposed as properties of the custom control, and presented in the Properties window.

Double clicking on the control at design-time creates a value-changed event handler, which is invoked when the user changes the value of the field:

private void cfDateTimePicker1_CFDateTimeValueChanged(object sender, DateTime NewValue)


Note that the event argument contains the newly changed value as a DateTime struct.

A small Windows Mobile Pocket PC application is included to demonstrate some basic uses of the control.

For those not familiar with the .xmta file, it defines the design-time properties of the control.

Points of interest

One issue that developers have encountered is having a Value property which has a default of DBNull.Value. This generally results in a NotSupportedException when the containing form loads. The reason for this is that, during design-time, when the control is dropped onto the form, Visual Studio gets the Value property and sets it into the resource file to be used as the default value upon form load. If the Value is DBNull.Value, this is inserted into the resources file, and causes the exception on form load. To get around this, I used the Site.DesignMode property of the control's parent class to detect when the control is in design mode, and for that one case, return a DateTime.Now value so that the resource file has a non-null value. During form load, I assert a private flag, IsInitializing, to ignore the value coming from the resource file.


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


About the Author

Member 4457245
Software Developer (Senior) Consulting contractor
United States United States
Medical device software

You may also be interested in...


Comments and Discussions

QuestionHow to compile? Pin
davidevit8115-May-08 11:21
memberdavidevit8115-May-08 11:21 
Generalgreat control! Pin
ausco19-Feb-08 14:08
memberausco19-Feb-08 14:08 
GeneralRe: great control! Pin
Member 445724525-Feb-08 1:10
memberMember 445724525-Feb-08 1:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 17 Feb 2008
Article Copyright 2008 by Member 4457245
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid