protected void ValidateTextInternal() { // if (mEdit == null) { Invalidate(); PropertyValue value = _ownerPropertyEnum.Property.Value; // Store the previous value string oldValue = value.GetStringValue(); if (oldValue != ActualText) { // Freeze painting because updating the property may also update other properties in the grid. _ownerPropertyEnum.Property.ParentGrid.BeginUpdate(); _ownerPropertyEnum.Property.Value.PreviousValue = _ownerPropertyEnum.Property.Value.GetValue(); value.UpdateStringValue(ActualText); // Defreeze painting _ownerPropertyEnum.Property.ParentGrid.EndUpdate(); // Notify the properties control if the value has really changed if (value.GetStringValue() != oldValue) { _ownerPropertyEnum.Property.ParentGrid.OnPropertyChanged(new PropertyChangedEventArgs(_ownerPropertyEnum)); } Parent.Invalidate(); } } }
public virtual PropertyValue.ValueValidationResult CommitChanges(bool final) { PropertyValue.ValueValidationResult changed = PropertyValue.ValueValidationResult.PreviousValueRestored; if (ReadOnly) { return(changed); } PropertyValue value = _ownerPropertyEnum.Property.Value; bool compareTextInCommitChanges = value.TypeConverter.CanConvertFrom(typeof(string)); bool textHasChanged = false; if (compareTextInCommitChanges) { textHasChanged = !value.DisplayString.Equals(Text); } object oldValue = null; object newValue = null; bool multipleValues = value.HasMultipleValues; if (!compareTextInCommitChanges || textHasChanged) { // Store the previous value if (!multipleValues) { oldValue = value.GetValue(); } try { if (!multipleValues || (Text.Length != 0)) { newValue = value.GetActualValue(Text); } } catch (Exception e) { _currentInvalidPropertyEnum = _ownerPropertyEnum; _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed; _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation( new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text, PropertyValue.ValueValidationResult.TypeConverterFailed, e)); if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore) { Text = value.DisplayString; _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored; _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation( new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, value.GetValue(), _currentValueValidationResult)); } return(_currentValueValidationResult); } } if ((multipleValues && textHasChanged) || ((newValue != null) && ((oldValue == null) || (newValue.Equals(oldValue) == false))) || ((newValue == null) && (oldValue != null))) { string str = Text; // Freeze painting because updating the property may also update other properties in the grid. // If the value is invalid based on the validator, all updated properties will be reverted back to // the previous value and a flicker would happen _ownerPropertyEnum.Property.ParentGrid.BeginUpdate(); _ownerPropertyEnum.Property.Value.PreviousValue = _ownerPropertyEnum.Property.Value.GetValue(); PropertyEnumerator invalidPropertyEnum; changed = value.SetValueFromInPlaceCtrl(str, out invalidPropertyEnum); if (PropertyValue.IsErrorCode(changed)) { _currentInvalidPropertyEnum = invalidPropertyEnum; } // Notify the grid if the value is invalid if (changed == PropertyValue.ValueValidationResult.ValidatorFailed) { OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation( new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, newValue, PropertyValue.ValueValidationResult.ValidatorFailed)); } else if (changed == PropertyValue.ValueValidationResult.ExceptionByClient) { OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation( new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, newValue, PropertyValue.ValueValidationResult.ExceptionByClient, value.LastExceptionByClient)); } // We have to update the edit control in case the value was refused or if its string representation // is different from what was typed (changes done by a CultureInfo for example) if ((_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore) || (changed == PropertyValue.ValueValidationResult.Validated)) { if (PasswordChar == 0) { Text = value.DisplayString; } else { Text = value.GetStringValue(); } } // If value is invalid, previous value is restored if ValueNotValidBehaviorMode is set accordingly if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore) { if (PropertyValue.IsErrorCode(changed)) { changed = PropertyValue.ValueValidationResult.PreviousValueRestored; _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation( new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, value.GetValue(), changed)); } } // Notify the properties control if the value has really changed if (changed == PropertyValue.ValueValidationResult.Validated) { if (PropertyValue.IsErrorCode(_currentValueValidationResult)) { _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation( new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, value.GetValue(), changed)); } _ownerPropertyEnum.Property.ParentGrid.NotifyPropertyChanged(new PropertyChangedEventArgs(_ownerPropertyEnum)); } _currentValueValidationResult = changed; // Defreeze painting and repaint _ownerPropertyEnum.Property.ParentGrid.EndUpdate(); } else { // Even if the value did not change, we ensure the text displayed is correct. A CultureInfo could // be involved for example that would change a modified text into the old value. Text = value.DisplayString; if (PropertyValue.IsErrorCode(_currentValueValidationResult)) { _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored; _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation( new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, value.GetValue(), _currentValueValidationResult)); } } return(changed); }