System.Windows.Forms.DateTimePicker Class

Represents a Windows control that allows the user to select a date and a time and to display the date and time with a specified format.

See Also: DateTimePicker Members

Syntax

[System.ComponentModel.Designer("System.Windows.Forms.Design.DateTimePickerDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
[System.ComponentModel.DefaultProperty("Value")]
[System.ComponentModel.DefaultEvent("ValueChanged")]
[System.Runtime.InteropServices.ComVisible(true)]
[System.ComponentModel.DefaultBindingProperty("Value")]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
public class DateTimePicker : Control

Remarks

The System.Windows.Forms.DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format. The System.Windows.Forms.DateTimePicker control makes it easy to work with dates and times because it handles a lot of the data validation automatically.

Note:

The System.Windows.Forms.DateTimePicker control only supports Gregorian calendars.

When used to represent a date, the System.Windows.Forms.DateTimePicker control appears in two parts: a drop-down list with a date represented in text, and a calendar that appears when you click the down-arrow next to the list. The calendar looks like the System.Windows.Forms.MonthCalendar control, which can be used for selecting multiple dates. For more information about the System.Windows.Forms.MonthCalendar control, see MonthCalendar Control Overview (Windows Forms).

You can change the look of the calendar portion of the control by setting the DateTimePicker.CalendarForeColor, DateTimePicker.CalendarFont, DateTimePicker.CalendarTitleBackColor, DateTimePicker.CalendarTitleForeColor, DateTimePicker.CalendarTrailingForeColor, and DateTimePicker.CalendarMonthBackground properties.

To use a spin button control (also known as an up-down control) to adjust the date/time value, set the DateTimePicker.ShowUpDown property to true. The calendar will not drop down when the control is selected. The date and time can be adjusted by selecting each element individually and using the up and down buttons to change the value.

The DateTimePicker.Value property contains the current date and time the control is set to. You can use the DateTimePicker.Text property or the appropriate member of DateTimePicker.Value to get the date and time value. For more information, see How to: Set and Return Dates with the Windows Forms DateTimePicker Control. You can limit the dates and times that can be selected by setting the DateTimePicker.MinDate and DateTimePicker.MaxDate properties.

The values can be displayed in four formats, which are set by the DateTimePicker.Format property: DateTimePickerFormat.Long, DateTimePickerFormat.Short, DateTimePickerFormat.Time, or DateTimePickerFormat.Custom. The default date DateTimePicker.Format is DateTimePickerFormat.Long.

If you want the System.Windows.Forms.DateTimePicker to appear as a control for picking or editing times instead of dates, set the DateTimePicker.ShowUpDown property to true and the DateTimePicker.Format property to DateTimePickerFormat.Time. For more information, see How to: Display Time with the DateTimePicker Control.

If the DateTimePicker.Format property is set to DateTimePickerFormat.Custom, you can create your own format style by setting the DateTimePicker.CustomFormat property and building a custom format string. The custom format string can be a combination of custom field characters and other literal characters. For example, you can display the date as "June 01, 2012 - Friday" by setting the DateTimePicker.CustomFormat property to "MMMM dd, yyyy - dddd". For more information, see How to: Display a Date in a Custom Format with the Windows Forms DateTimePicker Control and Custom Date and Time Format Strings.

Note:

When a System.Windows.Forms.DateTimePicker is data-bound and the backing value is changed to null, the value of the System.Windows.Forms.DateTimePicker will not be updated and the previous value will be retained. In situations where this behavior is not desirable (for example, when using a set of data-bound controls to page through a recordset) use the Binding.Format event of the System.Windows.Forms.Binding class to set the System.Windows.Forms.DateTimePicker to a value recognizable as a null.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0