设置 DateTimePicker 值错误

Setting DateTimePicker Value Error

我(从参考文献中)知道 DateTimePicker 的值可以设置如下:

DateTimePicker exampleDtPicker = new DateTimePicker();
exampleDtPicker.Value = new DateTime(2010, 10, 6);

但是,这不会更改表单上显示的值,而是显示当前本地日期。 与 exampleDtPicker 默认值不同的属性是否会影响 DateTimePicker 的设置值?这些是 exampleDtPicker 的属性:

DateTimePicker exampleDtPicker = new DateTimePicker() {
    Checked = false,
    Format = DateTimePickerFormat.Short;
    ShowCheckBox = true;
}

导致问题的原因是什么以及如何解决?

您的 DateTimePicker 已将 属性 Checked 设置为 false。通过这样的设置,如果您将其 ShowCheckBox 设置为 false 其他地方(并且可能即使您不这样做)......无论发生什么,它都可能永远不会更新自己 Value 你传进去了。来自MSDN:

Gets or sets a value indicating whether the Value property has been set with a valid date/time value and the displayed value is able to be updated.

和:

This property is used to obtain the state of the check box that is displayed if the P:System.Windows.Forms.DateTimePicker.ShowCheckBox property value is true. If the P:System.Windows.Forms.DateTimePicker.Checked property value is true, the T:System.Windows.Forms.DateTimePicker control displays the properly formatted P:System.Windows.Forms.DateTimePicker.Value property value; otherwise, the control displays the last valid date/time value assigned to the P:System.Windows.Forms.DateTimePicker.Value property or the current date and time (P:System.DateTime.Now) if no value has ever been assigned.