2 questions with raddatetimepicker

1 Answer 18 Views
DateTimePicker
darin
Top achievements
Rank 1
Iron
Iron
darin asked on 06 Mar 2022, 06:06 PM

We were using the built-in windows DateTimePicker for all of our dates, and have changed to using RadDateTimePicker, and i am having a few issues/questions.

1. We have Overrides setup on KeyPressEventArgs to trap the ENTER key which then sends a TAB - so our users can just hit enter to move to the next field instead of being required to hit tab (this works on all data entry fields except raddatetimepicker).

2. The user is required to type in the full year. We have the format set to Short. Using standard windows DateTimePicker, the user could type 22 and it would make it 2022. But, if the user types 22 now, it just puts the 22 in the year. So if the year was 2003 and you type in 22, the year now becomes 0322. We want the year to be entered as 2 digits and use the current century. How would we do that?

3. We don't want the user to be able to clear out the date completely, we want a date to be entered. Windows didn't allow this (we never set min date values). I even set the NullDate to be "1/1/1901" and that made it when i "cleared out" the date it displayed 1/1/0001.

Thanks for your help.

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 09 Mar 2022, 12:58 PM

Hello Darin,

Thank you for contacting us. I will go straight to your questions.

1. We have Overrides setup on KeyPressEventArgs to trap the ENTER key which then sends a TAB - so our users can just hit enter to move to the next field instead of being required to hit tab (this works on all data entry fields except raddatetimepicker).

The enter key is handled in the internal code of the control. What you can try is to subscribe to the KeyPress event and execute the tab navigation there.

private void RadDateTimePicker1_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == '\r')
    {
        e.Handled = true;
        SendKeys.Send("{TAB}");
    }
}
 2. The user is required to type in the full year. We have the format set to Short. Using standard windows DateTimePicker, the user could type 22 and it would make it 2022. But, if the user types 22 now, it just puts the 22 in the year. So if the year was 2003 and you type in 22, the year now becomes 0322. We want the year to be entered as 2 digits and use the current century. How would we do that?

You can check the Free Form Date Time parsing help article which I think is what you are looking for. When the user types 22 as a year, the parsing algorithm will convert it to the current century. However, when using this functionality the user needs to type valid date format. 

3. We don't want the user to be able to clear out the date completely, we want a date to be entered. Windows didn't allow this (we never set min date values). I even set the NullDate to be "1/1/1901" and that made it when i "cleared out" the date it displayed 1/1/0001.

What you can try here is to subscribe to the ValueChanged event. Inside the event handler, you can check the current date of the control. If the date is not valid for you, you can change the value property of the control.

private void RadDateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    if (this.radDateTimePicker1.Value.Year < 1909)
    {
        this.radDateTimePicker1.Value = new DateTime(1909, 1, 15, 0, 0, 0, 0);               
    }
}

I hope that I was able to cover your questions.

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

darin
Top achievements
Rank 1
Iron
Iron
commented on 09 Mar 2022, 01:14 PM

For the first issue, i have:

I have that written into my class, yet it is not being fired (like it is for textboxes, checkboxes, and all other controls). The MSGBOX in both never appears.

Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        MsgBox(DRInt(e.KeyChar))
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        MsgBox(e.KeyValue)
        If e.KeyData = Keys.Enter Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        Else
            MyBase.OnKeyDown(e)
        End If
    End Sub

 

For the second issue, i will look at that link to see if that helps.

Thanks.

Dinko | Tech Support Engineer
Telerik team
commented on 10 Mar 2022, 10:23 AM

I am not exactly sure why the KeyPress event handler is not called. Looking at the shared code snippet, you are overriding the KeyPress method. Are you using custom RadDateTimePicker? If yes, can you share it? 

Another approach that you can try is to create a custom MaskDateTimeProvider and override its KeyPress method.

Public Sub New()
        InitializeComponent()
        Me.radDateTimePicker1.DateTimePickerElement.TextBoxElement.Provider = New CustomMaskDateTimeProvider(Me.radDateTimePicker1.DateTimePickerElement.TextBoxElement.Mask, Me.radDateTimePicker1.Culture, Me.radDateTimePicker1.DateTimePickerElement.TextBoxElement)
    End Sub

    Public Class CustomMaskDateTimeProvider
        Inherits MaskDateTimeProvider

        Public Sub New(ByVal mask As String, ByVal culture As CultureInfo, ByVal owner As RadMaskedEditBoxElement)
            MyBase.New(mask, culture, owner)
        End Sub

        Public Overrides Sub KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            If  e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
                e.Handled = True
            End If

            MyBase.KeyPress(sender, e)
        End Sub

Give this approach a try and let me know how it goes.

Tags
DateTimePicker
Asked by
darin
Top achievements
Rank 1
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or