שתף באמצעות


Edit date in DateTimePicker with Backspace / Delete key

Question

Monday, October 22, 2018 7:28 AM

Hello MSDN forum,

Is it possible to correct/edit DateTimePicker with delete key or backspace key?

Example: I mean to write 2018 but end up with 2918. So when the moment I type '9' i realize i type incorrect and want to use backspace to remove it. Is that possible? 

So far, I try the keydown event with e.keychar = 8. It kinda work for backspace but i am unable to figure out the code to remove the front text one by one. 

I think it might involve with location of cursor on text but i cant seem to find guide for it. Please give me some way

All replies (7)

Monday, October 22, 2018 7:53 AM

Hi,

You can enter the value manually after clicking it after running.

 dtpTime.Format = DateTimePickerFormat.Custom
        dtpTime.CustomFormat = ""
 

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Monday, October 22, 2018 8:12 AM

Yes, i understand that. But what if user type incorrect and want to backspace to correct/edit?


Monday, October 22, 2018 8:38 AM

Hi,

Clear data

 Private Sub dtpTime_ValueChanged(sender As Object, e As EventArgs) Handles dtpTime.ValueChanged
        dtpTime.Format = DateTimePickerFormat.Custom
    End Sub

    Private Sub dtpTime_KeyDown(sender As Object, e As KeyEventArgs) Handles dtpTime.KeyDown
        If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
            dtpTime.Format = DateTimePickerFormat.Custom
            dtpTime.CustomFormat = " "
        End If
    End Sub

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Wednesday, October 24, 2018 4:11 AM

I know about the clear date with key press. But what i want is that i try to get Backspace key/Delete Key to edit (remove the letter) in datetimepicker box


Thursday, October 25, 2018 7:46 AM

Yes, i understand that. But what if user type incorrect and want to backspace to correct/edit?

Hi,

It seems that we can only modify the data like this.

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Thursday, October 25, 2018 8:40 AM

Hello Alex ,

I have a question about using Delete or Back.

Works great , but maybe i am doing something wrong or missing something.

When i select a date lets say today , and use Delete it clears the Datetimepicker.

But when i select the date again of today , nothing is showing .

Have to select another date first and then back select today .

 
 Private Sub Datumgemeldlossen_KeyDown(sender As Object, e As KeyEventArgs) Handles Datumgemeldlossen.KeyDown
        If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
            Datumgemeldlossen.Format = DateTimePickerFormat.Custom
            Datumgemeldlossen.CustomFormat = " "
        End If
 
    End Sub

Because i have a lot of datetimepickers i use this first.

  Private Sub SetDateTimePickerFormat(Optional format As String = " ", Optional container As Control = Nothing)

        If container Is Nothing Then container = Me
        For Each c As Control In container.Controls()
            If TypeOf c Is DateTimePicker Then
                Dim d As DateTimePicker = DirectCast(c, DateTimePicker)
                d.CustomFormat = format
                d.Format = DateTimePickerFormat.Custom
                If d.Tag Is Nothing Then
                    'only once
                    AddHandler d.ValueChanged, AddressOf DateTimePickerS_ValueChanged
                    d.Tag = "has handler"
                End If
            ElseIf c.Controls.Count > 0 Then 'container           
                SetDateTimePickerFormat(format, c)
            End If
        Next
    End Sub

    Private Sub DateTimePickerS_ValueChanged(sender As Object,
                                             e As EventArgs)

        Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
        If dtp.CustomFormat.Trim = "" Then
            dtp.CustomFormat = "dd-MM-yyyy"
        End If

    End Sub

Thursday, October 25, 2018 9:07 AM

You can use DTS_APPCANPARSE style,

but all the field will be editable and you will have to valid it in DTN_USERSTRING notification (WM_REFLECT + WM_NOTIFY in an inherited DTP class)