This is a migrated thread and some comments may be shown as answers.

I need to duplicate Teleriks time parsing logic

1 Answer 33 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 07 Jul 2011, 02:04 AM
Hi, due to the fact that slashes are required for date time entry (which i agree with others that its a pain to require it) I had to implement my own parsing logic by overriding OnParseDateTime.

By doing this we were able to support our custom date formats that do not require slashes but now we need to be able to type in the time formats as they exist in the control. 

We have determined with the way we are parsing the string we would need to come up with about 100 different formats to cover all of the formats that the control supports by default with its default parsing.

Is there any way to get the existing parsing logic built into the control so i can utilize the same time parsing portion of it?

1 Answer, 1 is accepted

Sort by
0
Pana
Telerik team
answered on 14 Jul 2011, 07:16 AM
Hello Jason,

Can not get easier than that:

Extend the RadDateTimePicker. Override its virutal parsing method:
public class CustomDateTimePicker : RadDateTimePicker
{
    protected override void OnParseDateTime(ParseDateTimeEventArgs e)
    {
        DateTime dt = new DateTime();
        bool isSuccessful = false;
        ParseDateTimeEventArgs pdtea = new ParseDateTimeEventArgs(e.RoutedEvent, e.TextToParse.Replace(".", "/"), e.PreviousValue, dt, isSuccessful);
        base.OnParseDateTime(pdtea);
        pdtea.Result = pdtea.Result;
    }
}

Replace all the "." with "/", create new fake ParseDateTimeEventArgs and use the base.OnParseDateTime to do the built in parsing for you.

Use the new CustomDateTimePicker like that:
<UserControl x:Class="DateTimePickerCustomParsing.MainPage"
        xmlns:local="clr-namespace:DateTimePickerCustomParsing;assembly=DateTimePickerCustomParsing">
    <Grid x:Name="LayoutRoot" Background="White">
        <local:CustomDateTimePicker x:Name="DTP" Width="150" Height="22" />
    </Grid>
</UserControl>

And the DateTimePicker will parse strings like 1.1.2001 to a date like 1/1/2001. The only problem is that once the date is parsed from string to DateTime other methods are used to render the DateTime back to string and "/" will appear on the place of the ".". You can change the DateTimePicker's culture in order to use . as separators but then "/" will be lost and converted to ".".

Best wishes,
Pana
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
DatePicker
Asked by
Jason
Top achievements
Rank 1
Answers by
Pana
Telerik team
Share this question
or