Hazmat Hazmat - 4 years ago 258
Vb.net Question

DateTimePicker does not displaying data even after I clicked a date to display

I have a DateTimePicker control on my WinForm with its CheckBox property enabled.
during the runtime when I want to select a date it does not show it in datetimepicker textbox, I know my code is correct cause the same code is working on the other form I have, but not in this one!

below is a snapshot of my code, any help is would be appreciated:

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


Reza Aghaie check I want like this

Answer Source

To handle change of that check box, you can use ValueChanged event:

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

In your code, you told it to don't show a date using DateTimePicker1.CustomFormat = " ".

See the comments that I added to your code below:

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
....
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download