How to Add and Customize a C# Date Time Picker

Working with a DateTimePicker Object in a Windows Application

© Mark Alexander Bain

Apr 27, 2009
The C# Date Time Picker, Mark Alexander Bain
A Date Time Picker is a visual way of choosing a date in a Windows application. This interactive calendar can added to a form and customized with C#.

The date time picker is a very useful variation of the ComboBox used in many Windows form based applications. It appears on the form as a ComboBox containing a date however, if the application user clicks on it, it doesn't show a drop-down list.

Instead it shows a calendar form which the user can select a date.

Once the user has selected a date then that is shown in the ComboBox and its value can be used elsewhere in the application. The C# programmer will, therefore, be pleased to learn that the date time picker is very easy to work with.

Adding a Date Time Picker to a C# Windows Application

A C# programmer adds a date time picker by creating a DateTimePicker object:

using System;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {
private DateTimePicker datePicker = new DateTimePicker ();

This object can then be added to the form:

public static void Main() {
Application.Run(new MainForm());
}
public MainForm() {
this.Controls.Add(datePicker);

If the form is compiled at this point then the user will see a simple form containing a DateTimePicker object, and if the user clicks on it then they will see the date time picker interactive calendar. However, the programmer can make some customizations to the way in which the object is displayed. For example they can:

  • change the location of the date time picker
  • change the format of the date shown in the date time picker

Fortunately the C# code to do all of this is quite simple.

Changing the Location of the Date Time Picker with C#

The date time picker will appear in the top left corner of the form by default. However, the programmer can change this by using the object's "Location" property:

datePicker.Location = new Point(50, 50);

Two values are passed to the point object used to set the location of the date time picker. These are the x and y coordinates and increasing them will move the object to the left and down the form.

Changing the Format of the Date Shown in the Date Time Picker

When the user opens the form then the date time picker will show then local system's long date format (for example "26 April 2006"). This can be changed to the short version (for example "26/4/2006"):

datePicker.Format = DateTimePickerFormat.Short;

Or the programmer can supply their own custom date format:

datePicker.CustomFormat = "dddd, d MMMM yyyy";
datePicker.Format = DateTimePickerFormat.Custom;

This time the date time picker will show something like "Sunday, 26 April 2009".

Using the User's Date Selection

Once the programmer has decided on a location and format for the date time picker then they can access the user's choice of date by accessing either of two properies:

  • Text
  • Value

The text propery contains the date as specified by the object's format and the value property contains a C# DateTime Object.

Summary

The C# DateTimePicker object provides a visual means of selecting a date in a Windows application. A programmer can:

  • change its location on a form
  • alter the format of the displayed date
  • access the object's text and value properties

And so they can supply their users with a means of selecting a date that is easy to use both by the user and the programmer.


The copyright of the article How to Add and Customize a C# Date Time Picker in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Add and Customize a C# Date Time Picker in print or online must be granted by the author in writing.


The C# Date Time Picker, Mark Alexander Bain
The C# Date Time Picker in a Form, Mark Alexander Bain
The C# Date Time Picker Interactive Calendar, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo