When C1Input detects an error while parsing or validating input value, it fires the ValidationError event. Then, by default, it shows an error message. The default behavior can be changed and customized in different ways:
C1Input controls have an ErrorInfo property containing settings (properties of the ErrorInfo class) affecting error handling:
Property | Description |
---|---|
BeepOnError | If True, the control beeps signaling an error. Default: False. |
CanLoseFocus | If True, the control is allowed to lose focus regardless of the error. This property is False by default, meaning that the control will stay in focus until the error is corrected. Note that setting ErrorAction to SetValueOnError or ResetValue allows the user to leave the control after error by resetting its value. |
ErrorAction | Enumerated value that determines what action is performed on the control value when an error occurs. ErrorAction set to None (default) means that the Value is not changed, remains as it was before the unsuccessful value update. If ErrorAction is set to SetValueOnError, control’s Value is set to the value specified in the ValueOnError property of the ErrorInfo class. If ErrorAction is set to ResetValue, control’s Value is set to the last value the control had before it entered edit mode. Setting the ErrorAction property to ThrowException interrupts execution and throws an exception, ValidationException. |
ErrorMessage | Error message shown in the standard message box and/or in the exception. |
ErrorMessageCaption | The text to display in the title bar of the error message box. |
ErrorProvider | Gets or sets an ErrorProvider object used to indicate error state of the control. |
ShowErrorMessage | If True (default), the standard error message is shown. |
ValueOnError | Value used to reset the control if ErrorAction is set to SetValueOnError. |
ValueOnErrorIsDbNull | Boolean property used to set ValueOnError to DBNull (only necessary at design time). |
In addition to that, ErrorInfo.ErrorMessage can be specified for particular actions: edit mask errors (MaskInfo.ErrorMessage), parsing (ParseInfo.ErrorMessage), pre- and post-validation (PreValidation.ErrorMessage, PostValidation.ErrorMessage). If a specialized error message is not specified in one of these sub-objects, the control’s ErrorInfo.ErrorMessage takes effect. Note that you can use ErrorProvider icon to indicate the error, instead of showing a message box, if you set ErrorProvider to an ErrorProvider component and ShowErrorMessage to False.
The properties listed above, when set in the control’s ErrorInfo object, affect all error handling in the control. When an error occurs, their values can be customized programmatically to handle that particular error. This is achieved by passing an ErrorInfo argument to the ValidationError event. The ErrorInfo argument passed to ValidationError is a copy of the control’s ErrorInfo with all its properties. It is an independent copy, so you can change properties in the ErrorInfo event argument for the current error without affecting the overall control’s ErrorInfo settings. By setting ErrorInfo properties in the ValidationError event you specify how to handle the error. For example, you can suppress the standard error message (and show your own message instead) by setting the ShowErrorMessage property to False, or you can change ValueOnError (and set ErrorAction to SetValueOnError), or change the ErrorMessage. Keep in mind that you must set the properties of the ErrorInfo argument passed to the ValidationError event, not the properties of the control’s ErrorMessage.
After the ValidationError event, error handling proceeds as specified in the event’s ErrorInfo argument. If ErrorAction is set to ThrowException, an exception is thrown (using the ErrorMessage text). If BeepOnError is True, the control beeps. If ShowErrorMessage is set to True, the standard error message box is shown (with ErrorMessage text). After that, the control’s value may be changed if so specified by ErrorAction. Finally, moving focus to another control is either canceled or permitted, according to CanLoseFocus, if validation was triggered by an attempt to move focus out of the control. If ErrorProvider property is set to an ErrorProvider component, that component is used to show the error icon near the offending control, with ErrorMessage ToolTip (ErrorProvider.SetError is called).
If you perform parsing or validation programmatically, in event code, and exit the event with an error condition (set the event’s Succeeded argument to False), you can describe the error and how it must be handled by setting the properties of an ErrorInfo argument passed to the event. Such argument is provided for the following events: PreValidating, Parsing and PostValidating. Its initial values are taken from the control’s ErrorInfo property. This ErrorInfo argument that you change in the event is then passed to the ValidationError event where it can be further changed as described above.