/// <summary> /// Raises the TextParseError event. /// </summary> /// <param name="e">A DatePickerTextParseErrorEventArgs that contains the event data.</param> protected virtual void OnTextParseError(DatePickerTextParseErrorEventArgs e) { EventHandler<DatePickerTextParseErrorEventArgs> handler = this.TextParseError; if (handler != null) { handler(this, e); } }
// iT SHOULD RETURN NULL IF THE STRING IS NOT VALID, RETURN THE DATETIME VALUE IF IT IS VALID /// <summary> /// Input text is parsed in the correct format and changed into a DateTime object. /// If the text can not be parsed TextParseError Event is thrown. /// </summary> private DateTime? ParseText(string text, DatePickerFormat f) { DateTime newSelectedDate; DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat; if (f == DatePickerFormat.Short) { try { newSelectedDate = DateTime.ParseExact(text,dtfi.ShortDatePattern, CultureInfo.CurrentCulture); return newSelectedDate; } catch(Exception ex) { DatePickerTextParseErrorEventArgs textParseError = new DatePickerTextParseErrorEventArgs(ex, text); OnTextParseError(textParseError); if (textParseError.ThrowException) { throw textParseError.Exception; } } } else { Debug.Assert(f == DatePickerFormat.Long); try { newSelectedDate = DateTime.ParseExact(text, dtfi.LongDatePattern, CultureInfo.CurrentCulture); return newSelectedDate; } catch (Exception ex) { DatePickerTextParseErrorEventArgs textParseError = new DatePickerTextParseErrorEventArgs(ex, text); OnTextParseError(textParseError); if (textParseError.ThrowException) { throw textParseError.Exception; } } } return null; }