Windows 窗体 DateTimePicker 控件中当前选定的日期或时间由 Value 属性确定。可以在显示此控件前(例如,在设计时或在 Form_Load 事件中)设置 Value 属性,确定此控件中开始时选定的日期。默认情况下,此控件的 Value 属性设置为当前日期。如果在代码中更改了此控件的 Value,则此控件将在窗体上自动更新以反映新设置。
Value 属性将 DateTime 结构作为它的值返回。有若干个 DateTime 结构的属性返回关于显示日期的特定信息。这些属性只能用于“返回”值;不要用它们来设置值。
对于日期值,Month、Day 和 Year 属性返回选定日期的这些时间单位的整数值。DayOfWeek 属性返回一个值,通过该值指示一周中的选定日(可能的值在 DayOfWeek 枚举中列出)。
对于时间值,Hour、Minute、Second 和 Millisecond 属性返回这些时间单位的整数值。
设置控件的日期和时间值
将 Value 属性设置为日期或时间值。
' Visual Basic
DateTimePicker1.Value = New DateTime(2001, 10, 20)
// C#
dateTimePicker1.Value = new DateTime(2001, 10, 20);
// C++
dateTimePicker1->Value = DateTime(2001, 10, 20);
返回日期和时间值
调用 Text 属性返回与控件中的格式相同的完整值,或调用 Value 属性的适当方法返回值的一部分。使用 ToString 将信息转换成可显示给用户的字符串。
' Visual Basic
MessageBox.Show("The selected value is ", DateTimePicker1.Text)
MessageBox.Show("The day of the week is ",
DateTimePicker1.Value.DayOfWeek.ToString)
MessageBox.Show("Millisecond is: ",
DateTimePicker1.Value.Millisecond.ToString)
// C#
MessageBox.Show ("The selected value is " +
dateTimePicker1.Text);
MessageBox.Show ("The day of the week is " +
dateTimePicker1.Value.DayOfWeek.ToString());
MessageBox.Show("Millisecond is: " +
dateTimePicker1.Value.Millisecond.ToString());
// C++
MessageBox::Show (String::Concat(S"The selected value is ",
dateTimePicker1->Text));
MessageBox::Show (String::Concat(S"The day of the week is ",
__box(dateTimePicker1->Value.DayOfWeek)->ToString()));
MessageBox::Show(String::Concat(S"Millisecond is: ",
(dateTimePicker1->Value.Millisecond).ToString()));
