DateTimePicker 类

表示一个 Windows 控件,该控件用来让用户选择日期和时间并以指定的格式显示此日期和时间。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public Class DateTimePicker
    Inherits Control
用法
Dim instance As DateTimePicker
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class DateTimePicker : Control
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
[ComVisibleAttribute(true)] 
public ref class DateTimePicker : public Control
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class DateTimePicker extends Control
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class DateTimePicker extends Control

备注

DateTimePicker 控件用来让用户选择日期和时间,并以指定的格式显示此日期和时间。通过设置 MinDateMaxDate 属性,可以限制可选择的日期和时间。

通过设置 CalendarForeColorCalendarFontCalendarTitleBackColorCalendarTitleForeColorCalendarTrailingForeColorCalendarMonthBackground 属性,可以更改控件日历部分的外观。

Format 属性设置控件的 DateTimePickerFormat。默认日期 FormatDateTimePickerFormat.Long。如果 Format 属性设置为 DateTimePickerFormat.Custom,可以通过设置 CustomFormat 属性并生成自定义格式字符串来创建自己的格式化样式。自定义格式字符串可以是自定义字段字符和其他字符的组合。例如,通过将 CustomFormat 属性设置为“MMMM dd, yyyy - dddd”,可以将日期显示为“June 01, 2001 - Friday”。有关更多信息,请参见 日期与时间格式字符串

若要使用数值调节钮控件(也称为 up-down 控件)调整日期/时间值,请将 ShowUpDown 属性设置为 true。日历控件被选定后将不下拉。可以通过分别选择各元素并使用向上和向下按钮更改值来调整日期和时间。

如果需要自定义日期格式设置(例如,选择限制为只使用一个日期),您可以考虑使用 DateTimePicker 控件,而不是 MonthCalendar。使用 DateTimePicker 可限制对日期/时间值进行大量数据验证的需要。

提示

DateTimePicker 控件只支持公历格式。

示例

下面的代码示例创建 DateTimePicker 控件的一个新实例并将其初始化。设置控件的 CustomFormat 属性。此外,还要设置 ShowCheckBox 属性以便此控件显示 CheckBox;并设置 ShowUpDown 属性以便此控件显示为数值调节钮控件(也称为 up-down 控件)。

Public Sub CreateMyDateTimePicker()
    ' Create a new DateTimePicker control and initialize it.
    Dim dateTimePicker1 As New DateTimePicker()
    
    ' Set the MinDate and MaxDate.
    dateTimePicker1.MinDate = New DateTime(1985, 6, 20)
    dateTimePicker1.MaxDate = DateTime.Today
    
    ' Set the CustomFormat string.
    dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd"
    dateTimePicker1.Format = DateTimePickerFormat.Custom
    
    ' Show the CheckBox and display the control as an up-down control.
    dateTimePicker1.ShowCheckBox = True
    dateTimePicker1.ShowUpDown = True
End Sub 'CreateMyDateTimePicker
public void CreateMyDateTimePicker()
{
   // Create a new DateTimePicker control and initialize it.
   DateTimePicker dateTimePicker1 = new DateTimePicker();

   // Set the MinDate and MaxDate.
   dateTimePicker1.MinDate = new DateTime(1985, 6, 20);
   dateTimePicker1.MaxDate = DateTime.Today;

   // Set the CustomFormat string.
   dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
   dateTimePicker1.Format = DateTimePickerFormat.Custom;

   // Show the CheckBox and display the control as an up-down control.
   dateTimePicker1.ShowCheckBox = true;
   dateTimePicker1.ShowUpDown = true;
}
   
public:
   void CreateMyDateTimePicker()
   {
      // Create a new DateTimePicker control and initialize it.
      DateTimePicker^ dateTimePicker1 = gcnew DateTimePicker;
      
      // Set the MinDate and MaxDate.
      dateTimePicker1->MinDate = DateTime(1985,6,20);
      dateTimePicker1->MaxDate = DateTime::Today;
      
      // Set the CustomFormat string.
      dateTimePicker1->CustomFormat = "MMMM dd, yyyy - dddd";
      dateTimePicker1->Format = DateTimePickerFormat::Custom;
      
      // Show the CheckBox and display the control as an up-down control.
      dateTimePicker1->ShowCheckBox = true;
      dateTimePicker1->ShowUpDown = true;
   }
public void CreateMyDateTimePicker()
{
    // Create a new DateTimePicker control and initialize it.
    DateTimePicker dateTimePicker1 = new DateTimePicker();

    // Set the MinDate and MaxDate.
    dateTimePicker1.set_MinDate(new DateTime(1985, 6, 20));
    dateTimePicker1.set_MaxDate(DateTime.get_Today());

    // Set the CustomFormat string.
    dateTimePicker1.set_CustomFormat("MMMM dd, yyyy - dddd");
    dateTimePicker1.set_Format(DateTimePickerFormat.Custom);

    // Show the CheckBox and display the control as an up-down control.
    dateTimePicker1.set_ShowCheckBox(true);
    dateTimePicker1.set_ShowUpDown(true);
} //CreateMyDateTimePicker

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
        System.Windows.Forms.DateTimePicker

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

DateTimePicker 成员
System.Windows.Forms 命名空间
Size