Click here to Skip to main content
6,295,667 members and growing! (15,300 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » Controls     Intermediate License: The Code Project Open License (CPOL)

Nullable Compact Framework DateTimePicker Custom Control

By Member 4457245

A custom DateTimePicker control for the Compact Framework, which handles DBNull values.
C# (C# 1.0, C# 2.0, C# 3.0), .NET CF, .NET (.NET 2.0), Visual Studio (VS2005), Dev, Design
Posted:17 Feb 2008
Views:12,688
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 2.35 Rating: 3.91 out of 5

1

2
1 vote, 25.0%
3
3 votes, 75.0%
4

5

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:

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)

About the Author

Member 4457245


Member
Medical device software
Occupation: Software Developer (Senior)
Company: Consulting contractor
Location: United States United States

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralHow to compile? Pinmemberdavidevit8112:21 15 May '08  
Generalgreat control! Pinmemberausco15:08 19 Feb '08  
GeneralRe: great control! PinmemberMember 44572452:10 25 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Feb 2008
Editor: Smitha Vijayan
Copyright 2008 by Member 4457245
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project