/// <summary> /// Raises the ParserError event when there is an error in parsing user input. /// </summary> /// <param name="e">Event args.</param> protected virtual void OnParseError(UpDownParseErrorEventArgs e) { if (ParseError != null) { ParseError(this, e); } }
protected internal override void ApplyValue(string text) { if (!IsEditable) { return; } // parsing brings us to edit mode. Expected is that we are already in editmode // but a derived class might call to parse directly. IsEditing = true; try { Value = ParseValue(text); } catch (Exception error) { UpDownParseErrorEventArgs args = new UpDownParseErrorEventArgs(text, error); OnParseError(args); if (!args.Handled) { // default error handling is to discard user input and revert to the old text SetTextBoxText(); } } finally { // at any rate, parsing means we move out of edit mode // except if this is invalid input and we are in the textbox cannot lose focus mode if (!IsInvalidInput || InvalidInputAction != InvalidInputAction.TextBoxCannotLoseFocus) { IsEditing = false; } } }
protected internal virtual void ApplyValue(string text) { UpDownParsingEventArgs <T> parsingArgs = new UpDownParsingEventArgs <T>(text); Exception ParsingException = null; T regularParsedValue = default(T); try { // parse value might throw an exception. regularParsedValue = ParseValue(text); parsingArgs.Value = regularParsedValue; } catch (Exception error) { ParsingException = error; } try { OnParsing(parsingArgs); } catch (Exception) { // swallow any exception during custom parsing. } if (ParsingException == null) { // if no error, regularParsedValue must be initialized. T newValue = parsingArgs.Handled ? parsingArgs.Value : regularParsedValue; if (Value.Equals(newValue)) { // although value might be the same, still format the value. SetTextBoxText(); } Value = newValue; } else { if (parsingArgs.Handled) { if (Value.Equals(parsingArgs.Value)) { // although value might be the same, still format the value. SetTextBoxText(); } Value = parsingArgs.Value; } else { UpDownParseErrorEventArgs args = new UpDownParseErrorEventArgs(text, ParsingException); OnParseError(args); if (!args.Handled) { // default error handling is to discard user input and revert to the old text SetTextBoxText(); } } } }
/// <summary> /// Raised when TimeUpDown raises this event. /// </summary> /// <param name="sender">The TimeUpDown instance raising this event.</param> /// <param name="e">The instance containing the event data.</param> /// <remarks>Here to make it easier to access /// these events.</remarks> private void TimeUpDownParseError(object sender, UpDownParseErrorEventArgs e) { EventHandler<UpDownParseErrorEventArgs> handler = ParseError; if (handler != null) { handler(this, e); } }