Export (0) Print
Expand All

Microsoft .NET Compact Framework-based DateTimePicker Control

.NET Compact Framework 1.0
 

Ralph Arvesen
Vertigo Software, Inc.

April 2003

Applies to:
    Microsoft® .NET Compact Framework 1.0
    Microsoft Visual Studio® .NET 2003

Summary: Learn how to use a managed DateTimePicker class in your Pocket PC projects. The .Net Compact Framework provides managed classes for most controls but the DateTimePicker class is not supported. Wrapper classes that P/Invoke the native control can be found on the web, but this sample provides a purely managed DateTimePicker class that you can use in your Pocket PC projects. The class and sample application are provided in C# and VB.NET. (6 printed pages)


Download DateTimePicker.msi.

Contents

Overview of the Managed Control
Properties and Events
What is not Supported
Using the Control
Conclusion
More Information

Overview of the Managed Control

The DateTimePicker control derives from the System.Windows.Forms.Control class and consists of two parts: a label that displays the selected date and a popup calendar that allows users to select a new date. The label part of the control is shown below.

Figure 1. The label part of the managed control

Clicking the label displays the popup calendar as shown below. This is a grid that shows the current month along with a few days from the previous and next months.

Figure 2. The popup calendar part of the managed control

The selected date is highlighted and today is outlined with a black square. The popup calendar supports the following actions:

  • Tapping a date selects the date and closes the popup calendar.
  • Tapping and dragging navigates new dates.
  • Pressing the device jog buttons navigates new dates.
  • Tapping the today label at the bottom selects today’s date.
  • Tapping the arrow buttons in the caption navigates months.
  • Tapping the month label in the caption displays a month context menu.
  • Tapping the year label in the caption displays a year updown control.

The month context menu and year updown controls are shown below.

Figure 3. Displaying the month context menu and year updown control

Properties and Events

In addition to the properties and events of the Control class, the managed control contains the following properties and events.

Properties Comments
Format A DateTimePickerFormat value that specifies if the format is short or long. Defaults to long.
MaxDateTime A read-only DateTime object that indicates the maximum date supported by the control.
MinDateTime A read-only DateTime object that indicates the minimum date supported by the control.
Text A string object that represents the selected date.
Value A DateTime object that represents the selected date.
Events Comments
CloseUp The popup calendar was dismissed.
DropDown The popup calendar was displayed.
ValueChanged The Value property has changed (user selected a new date).

What is not Supported

The sample control does not support all of the features of the full framework’s DateTimePicker class. The following features are not supported:

  • Cannot set or display times; only dates are supported.
  • Cannot set the date with the label control; the popup calendar must be used to select dates.
  • Cannot specify custom formatting; only short and long formats are supported.

Using the Control

The sample application demonstrates how to use the managed control by creating and positioning the control, changing properties and handling events. The sample application is shown below.

Figure 4. Sample application that uses the managed control

Creating and Positioning the Control

First, a DateTimePicker class is declared.

C#

DateTimePicker m_picker;

VB.NET

Dim WithEvents m_picker As DateTimePicker

Next, the control is created and positioned. A useful technique is to use a label control as a placeholder; this allows you to use the GUI designer to position and size the control instead of hard coding values.

C#

m_picker = new DateTimePicker();
m_picker.Location = labelPlaceHolder.Location;
m_picker.Size = labelPlaceHolder.Size;
labelPlaceHolder.Parent.Controls.Add(m_picker);
labelPlaceHolder.Parent.Controls.Remove(labelPlaceHolder);

VB.NET

m_picker = New DateTimePicker
m_picker.Location = labelPlaceHolder.Location
m_picker.Size = labelPlaceHolder.Size
labelPlaceHolder.Parent.Controls.Add(m_picker)
labelPlaceHolder.Parent.Controls.Remove(labelPlaceHolder)

Handling Events

The WithEvents keyword can be used to hookup events in VB.NET but you need to append event handlers in C#.

C#

m_picker.ValueChanged += new EventHandler(OnValueChanged);
m_picker.DropDown += new EventHandler(OnDropDown);
m_picker.CloseUp += new EventHandler(OnCloseUp);

The ValueChanged event handler logs events to a list and is shown below.

C#

private void OnValueChanged(System.Object sender, System.EventArgs e)
{
   listEvents.Items.Add("ValueChanged - " + 
      m_picker.Value.ToShortDateString());
   listEvents.SelectedIndex = listEvents.Items.Count - 1;
}

VB.NET

Private Sub OnValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles m_picker.ValueChanged
   listEvents.Items.Add(("ValueChanged - " + _
      m_picker.Value.ToShortDateString()))
   listEvents.SelectedIndex = listEvents.Items.Count - 1
End Sub

Conclusion

The managed control source contains a DateTimePickerFormat enumeration (used for the Format property), DateTimePicker class (the label part of the control) and a DayPickerPopup class (the popup calendar). Double buffering is used when drawing to prevent flashing and information (data, arrow coordinates, hit rectangles, and GDI objects) are cached for better performance. You can extend the control by modifying the source or deriving your own class from the control.

More Information

For further information, please see the following resources:

Show:
© 2014 Microsoft