public ref class DateTimePicker : System::Windows::Forms::Control
public class DateTimePicker : System.Windows.Forms.Control
[System.ComponentModel.DefaultBindingProperty("Value")]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
public class DateTimePicker : System.Windows.Forms.Control
[System.ComponentModel.DefaultBindingProperty("Value")]
public class DateTimePicker : System.Windows.Forms.Control
type DateTimePicker = class
inherit Control
[<System.ComponentModel.DefaultBindingProperty("Value")>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DateTimePicker = class
inherit Control
[<System.ComponentModel.DefaultBindingProperty("Value")>]
type DateTimePicker = class
inherit Control
Public Class DateTimePicker
Inherits Control
DateTimePicker
下面的代码示例创建控件的新实例
DateTimePicker
并对其进行初始化。 控件的
CustomFormat
属性已设置。 此外,
ShowCheckBox
设置 属性,以便控件显示
CheckBox
,设置
ShowUpDown
属性,以便控件显示为旋转按钮控件。 若要运行此示例,请将以下代码粘贴到窗体中,并在示例窗体的构造函数或
Load
事件处理方法中调用
CreateMyDateTimePicker
方法。
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.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 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
控件 DateTimePicker 用于允许用户选择日期和时间,以及以指定格式显示该日期和时间。 控件 DateTimePicker 可以轻松处理日期和时间,因为它会自动处理大量数据验证。
控件 DateTimePicker 仅支持公历。
当用于表示日期时, DateTimePicker 控件将按两部分显示:一个以文本表示日期的下拉列表,以及一个在单击列表旁边的向下箭头时显示的日历。 日历类似于 MonthCalendar 控件,可用于选择多个日期。 有关控件的详细信息 MonthCalendar ,请参阅 MonthCalendar 控件概述。
可以通过设置 CalendarForeColor、、、CalendarTitleForeColorCalendarTrailingForeColorCalendarFontCalendarTitleBackColor、 和 CalendarMonthBackground 属性来更改控件的日历部分的外观。
若要使用旋转按钮控件 (也称为向上向下控件) 调整日期/时间值,请将 属性设置为 ShowUpDowntrue
。 选择控件时,日历不会下拉。 可以通过单独选择每个元素并使用向上和向下按钮更改值来调整日期和时间。
Value 属性包含控件设置的当前日期和时间。 可以使用 属性 Text 或 的相应成员 Value 来获取日期和时间值。 有关详细信息,请参阅如何:使用Windows 窗体 DateTimePicker 控件设置和返回日期。 可以通过设置 和 MaxDate 属性来限制可以选择的MinDate日期和时间。
这些值可以采用四种格式显示,这些格式由 Format 属性设置:Long、Short、Time 或 Custom。 默认日期 Format 为 DateTimePickerFormat.Long。
如果希望 DateTimePicker 显示为用于选取或编辑时间的控件而不是日期,请将 属性设置为 ShowUpDowntrue
,将 Format 属性设置为 Time。 有关详细信息,请参阅 如何:使用 DateTimePicker 控件显示时间。
如果 属性 Format 设置为 DateTimePickerFormat.Custom,则可以通过设置 CustomFormat 属性并生成自定义格式字符串来创建自己的格式样式。 自定义格式字符串可以是自定义字段字符和其他文本字符的组合。 例如,通过将 属性设置为 CustomFormat “MMMM dd, yyyy - d”,可以将日期显示为“2012 年 6 月 1 日 - 星期五”。 有关详细信息,请参阅如何:使用Windows 窗体 DateTimePicker 控件以自定义格式显示日期和自定义日期和时间格式字符串。
DateTimePicker当 数据绑定并且支持值更改为 null
时,将不会更新 的值DateTimePicker,并且将保留以前的值。 例如,在不需要此行为的情况下, (使用一组数据绑定控件分页浏览记录集) 使用 Format 类的 Binding 事件将 设置为 DateTimePicker 可识别为 null
的值。