我的WinForm上有一个DateTimePicker控件,启用了CheckBox属性。 在运行时,当我想选择一个日期时它没有在datetimepicker文本框中显示它,我知道我的代码是正确的,因为相同的代码正在处理我有的另一种形式,但不是在这一个!
下面是我的代码的快照,任何帮助都将不胜感激:
Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
DateTimePicker1.Enabled = True
DateTimerPicker1.Checked = False
If DateTimePicker1.Checked = False Then
DateTimePicker1.CustomFormat = " "
DateTimePicker1.Format = DateTimePickerFormat.Custom
Else
DateTimePicker1.Format = DateTimePickerFormat.Short
End If
End Sub
答案 0 :(得分:1)
要处理该复选框的更改,您可以使用ValueChanged
事件:
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) _
Handles DateTimePicker1.ValueChanged
If Me.DateTimePicker1.Checked=True Then
Me.DateTimePicker1.Format = DateTimePickerFormat.Short
Else
Me.DateTimePicker1.Format = DateTimePickerFormat.Custom
Me.DateTimePicker1.CustomFormat = " "
End If
End Sub
在您的代码中,您告诉它不要使用DateTimePicker1.CustomFormat = " "
显示日期。
请参阅我在下面的代码中添加的评论:
DateTimerPicker1.Checked = False '<==== You set Checked to false
If DateTimePicker1.Checked = False Then '<==== You told if not checked (it's not checked)
DateTimePicker1.CustomFormat = " " '<==== Show space
DateTimePicker1.Format = DateTimePickerFormat.Custom
....