none
Clear Datetimepicker and select same date RRS feed

  • Question

  • I found a little problem and cannot figure it out.

    On Form load i clear the datetimepicker and select a date let say today, with Delete i clear it again.

    But when i select later on with out closing the programm the date of today nothing happens ?

    I have to select another date first and then i can select the date of today again.

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            DateTimePicker1.CustomFormat = " "
            DateTimePicker1.Format = DateTimePickerFormat.[Custom]
    
        End Sub
    
        Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
            If DateTimePicker1.CustomFormat = " " Then
                DateTimePicker1.CustomFormat = "dd-MM-yyyy"
            End If
        End Sub
    
        Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown
            If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
                DateTimePicker1.CustomFormat = " "
                DateTimePicker1.Format = DateTimePickerFormat.[Custom]
            End If
        End Sub


    Sunday, October 28, 2018 3:17 PM

Answers

  • Hi

    OK, then try this:

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DateTimePicker1
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "dd-MM-yyyy"
        End With
      End Sub
      Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown
        If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
          DateTimePicker1.CustomFormat = " "
        End If
      End Sub
      Private Sub DateTimePicker1_MouseDown(sender As Object, e As MouseEventArgs) Handles DateTimePicker1.MouseDown
        DateTimePicker1.CustomFormat = "dd-MM-yyyy"
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by KeesBlunder Sunday, October 28, 2018 6:49 PM
    Sunday, October 28, 2018 4:45 PM

All replies

  • Hi

    I am not sure I understand what you need, bit, I suspect it isn't what you are doing there.

    Why are you changing the Format side of things? Normally, you would set the format once in (say) the Form Load event as you started to do, and from then on,just use the GateTimePicker.Value to read/set the DateTime from it.

    Image

    Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DateTimePicker1
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "dd-MM-yyyy"
        End With
      End Sub
      Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        Label1.Text = DateTimePicker1.Value.ToString
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, October 28, 2018 3:40 PM Addedmimage and Code
    Sunday, October 28, 2018 3:29 PM
  • Hello Les,

    On start this 

    this is the format when i select a date , of today.

    When i clear the datetimepicker again with delete or backspace.

    So far it works the way i like to use it , but when i want to select the dat of today again .

    There is a problem , i cannot .

    Have to select another date before  i can select the date of today again.

    Sunday, October 28, 2018 3:56 PM
  • Hi

    OK, then try this:

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DateTimePicker1
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "dd-MM-yyyy"
        End With
      End Sub
      Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown
        If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
          DateTimePicker1.CustomFormat = " "
        End If
      End Sub
      Private Sub DateTimePicker1_MouseDown(sender As Object, e As MouseEventArgs) Handles DateTimePicker1.MouseDown
        DateTimePicker1.CustomFormat = "dd-MM-yyyy"
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by KeesBlunder Sunday, October 28, 2018 6:49 PM
    Sunday, October 28, 2018 4:45 PM
  • Maybe handle DropDown event and use a code that is similar to ValueChanged.


    Sunday, October 28, 2018 4:48 PM
  • Thanks Les,

    That works great , i was looking in the change event but MouseDown is the one.

    I only change the .CustomFormat in Formload so it is starting emtpy.

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With DateTimePicker1
                .Format = DateTimePickerFormat.Custom
                .CustomFormat = " "
            End With
        End Sub
        Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown
            If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
                DateTimePicker1.CustomFormat = " "
            End If
        End Sub
        Private Sub DateTimePicker1_MouseDown(sender As Object, e As MouseEventArgs) Handles DateTimePicker1.MouseDown
            DateTimePicker1.CustomFormat = "dd-MM-yyyy"
        End Sub
    Thanks.

    Sunday, October 28, 2018 6:51 PM