# Validating the values

MSPG uses a TypeConverter to transform user-entered strings into values. If an exception is raised, it displays a generic popup form. The SPG library, however, is able to catch three types of errors, enabling you to handle them as required:

  • Exceptions raised by a TypeConverter.
  • Exceptions generated by the set accessor of the client application’s property.
  • Failed validations resulting from a special Validator class.

Each has an error code, which is transmitted to the client application when the ValueValidation event is raised).

# Error codes

These are the error codes for PropertyValue.ValueValidationResult:

Error code Description
TypeConverterFailed The failed validation originates from a TypeConverter that raised an exception.
ValidatorFailed The Check method of the validator attached to the Property returned “False”.
ExceptionByClient An exception occurred in the client application code (inside a set accessor method).

The following codes are part of the same enumeration, and are used when the second, and subsequent, ValueValidation event is sent to the client application:

Error code Description
Validated A subsequently entered value has been successfully validated.
PreviousValueRestored A previously valid value has been restored.

# Event handler

The event handler receives a ValueValidationEventArgs parameter. Its properties enable the client application to determine what happened:

Property Description
PropertyEnum An enumerator to the property edited by the user.
InvalidPropEnum An enumerator to the invalid property. This usually equals RightBound, indicating that the property referenced by PropertyEnum is the invalid one. However, it points to a child if its value has become invalid due to changes to the parent’s value.
ValueValidationResult One of the error codes explained in Error codes, above.
ValueToValidate The invalid value.
Exception References the exception generated, if any.
Message A string message set by the validator class, or equal to the message given by the exception.

# Validation modes

The PropertyGrid.ValueNotValidBehaviorMode property enables the client application to choose between two modes from the PropertyGrid.ValueNotValidBehaviorModes enumeration:

Enum value Description
IgnoreAndRestore Any invalid value is instantly replaced by the previous valid value. The client application receives two notifications: the invalid code, and the PreviousValueRestored code.
KeepFocus When the user tries to leave the inplace control containing an invalid value, the focus is forced on the control, and the proper notification is sent to the client application.
In this mode, when an error code is sent, the client application can choose how to react, for example display a smart dialog, a simple message box, a custom tooltip, or play a beep.

# Validator classes

As discussed above, an error can originate from the check performed by a validator class derived from PropertyValidatorBase, e.g. the class PropertyValidatorMinMax.

The PropertyValidatorAttribute attribute can set a validator for a Property discovered by reflection. You can also set a validator at runtime.

# To set a validator at runtime

propEnum.Property.Value.Validator = new PropertyValidatorMinMax(0, 300);

# When writing your own validator

Override the virtual method contained by the PropertyValidatorBase class:

public virtual bool Check(object value, bool modify);
Parameter Description
value The value to be checked.
modify If true, your code should call PropertyValue.SetValue with an appropriate value. This avoids initializing a Property with an invalid value.
Returns Returns true if the value is valid, false otherwise.
Last Updated: 5/25/2022, 1:18:09 PM