How to implement custom parsing

The purpose of this tutorial is to show you how to use the RadDateTimePicker, implementing your own logic for parsing the value in a way that suits your needs.

The following example will allow you to enter a month's name into the RadDateTimePicker's input box. As a result the control will assume that the chosen date is the first day of that month and the year will be equal to the current year. Whenever the user enters some text that cannot be parsed it will simply show you an error preview tooltip.

Read more about the RadDateTimePicker's events here.

The final result should look like the snapshots below:

Silverlight RadDateTimePicker Custom Parsing

You can do this by:

  • Handling the ParseDateTimeValue event.

  • Get the user-entered value of the RadDateTimePicker input box control using the ParseDateTimeEventArgs parameter's TextToParse property.

  • Set the ParseDateTimeEventArgs parameter's Result property whenever the parsing is successful.

  • Set the ParseDateTimeEventArgs parameter's IsParsingSuccessful property to False to indicate an error.

When you enter some text in the control's input box the ParseDateTimeValue event is fired and tries to parse the input. If the parsing is possible it sets the ParseDateTimeEventArgs parameter's IsParsingSucceful property to True. If in this case you hit the Enter key on your keyboard or lose focus in any other way, the SelectionChanged event is invoked.

Using the ParseDateTimeValue event

First you have to define your RadDateTimePicker control and point out that you want to implement the ParseDateTimeValue event.

<telerik:RadDateTimePicker x:Name="radDateTimePicker"  
                           ParseDateTimeValue="radDateTimePicker_ParseDateTimeValue"/> 

Then in the event handler you can provide the parsing logic. For the current example you have to get the entered text and check if it represents a valid month's name.

private void radDateTimePicker_ParseDateTimeValue(object sender, Telerik.Windows.Controls.ParseDateTimeEventArgs args) 
{ 
    string input = args.TextToParse.ToLower(); 
    System.Globalization.DateTimeFormatInfo formatInfo = new System.Globalization.DateTimeFormatInfo(); 
    int monthIndex = 1; 
    foreach (string month in formatInfo.MonthNames) 
    { 
        if (input.Contains(month.ToLower()) == true) 
            break; 
        monthIndex++; 
    } 
    if (monthIndex < 12) 
    { 
        args.Result = new DateTime(2010, monthIndex, 1); 
    } 
    else 
    { 
        args.IsParsingSuccessful = false; 
    } 
} 
Private Sub radDateTimePicker_ParseDateTimeValue(sender As Object, args As Telerik.Windows.Controls.ParseDateTimeEventArgs) 
    Dim input As String = args.TextToParse.ToLower() 
    Dim formatInfo As New System.Globalization.DateTimeFormatInfo() 
    Dim monthIndex As Integer = 1 
    For Each month As String In formatInfo.MonthNames 
        If input.Contains(month.ToLower()) = True Then 
            Exit For 
        End If 
        monthIndex += 1 
    Next 
    If monthIndex < 12 Then 
        args.Result = New DateTime(2010, monthIndex, 1) 
    Else 
        args.IsParsingSuccessful = False 
    End If 
End Sub 

Here is the result:

Silverlight RadDateTimePicker Custom Parsing

If you want to validate the input date in code behind using ParseDateTimeValue event and set the date back to a previous value if the validation fails, you can still do this. This time you have to set the ParseDateTimeEventArgs parameter's IsParsingSucceful property to True no matter of the errors. However, when validation errors occur you can set the Result property to ParseDateTimeEventArgs parameter's PreviousValue property. In this way if you enter some invalid data, the Preview Tooltip will show you the PreviousValue property's value which will be the last successful input. If you hit enter now, the RadDateTimePicker will select the value indicated by the Preview Tooltip.

See Also

In this article