Click here to Skip to main content
15,868,236 members
Articles / Programming Languages / C#

Nullable Compact Framework DateTimePicker Custom Control

Rate me:
Please Sign up or sign in to vote.
3.91/5 (4 votes)
17 Feb 2008CPOL3 min read 40.3K   717   18   3
A custom DateTimePicker control for the Compact Framework, which handles DBNull values.

Introduction

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.

Background

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:

C#
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.

License

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


Written By
Software Developer (Senior) Consulting contractor
United States United States
Medical device software

Comments and Discussions

 
QuestionHow to compile? Pin
davidevit8115-May-08 11:21
davidevit8115-May-08 11:21 
Generalgreat control! Pin
afraan19-Feb-08 14:08
afraan19-Feb-08 14:08 
GeneralRe: great control! Pin
Member 445724525-Feb-08 1:10
Member 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.