public PropertyValidatorAttribute(string childPropertyName, Type validatorType, params object[] parameters)
        {
            if (childPropertyName == null)
                throw new ArgumentException("The name of the child property can't be null");

            _childPropertyName = childPropertyName;
            _validator = (Activator.CreateInstance(validatorType, parameters) as PropertyValidatorBase);
        }
示例#2
0
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == Win32Calls.WM_KEYDOWN)
            {
                Keys key = (Keys)(int)m.WParam;
                if ((key == Keys.Down) || (key == Keys.Up) || (key == Keys.PageDown) || (key == Keys.PageUp))
                {
                    if ((_edit != null) && _edit.Focused)
                    {
                        // Check to see if the current displayed value is valid regarding its TypeConverter
                        object valueToValidate = null;
                        try
                        {
                            valueToValidate = _ownerPropertyEnum.Property.Value.GetValueToValidate(Text);
                        }
                        catch (Exception e)
                        {
                            _edit.SelectAll();
                            _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                            _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, OwnerPropertyEnumerator,
                                                             Text, PropertyValue.ValueValidationResult.TypeConverterFailed, e));
                            return(true);
                        }

                        int newValue = (int)valueToValidate;
                        if (key == Keys.Down)
                        {
                            newValue -= _trackbar.SmallChange;
                        }
                        else if (key == Keys.Up)
                        {
                            newValue += _trackbar.SmallChange;
                        }
                        else if (key == Keys.PageDown)
                        {
                            newValue -= _trackbar.LargeChange;
                        }
                        else if (key == Keys.PageUp)
                        {
                            newValue += _trackbar.LargeChange;
                        }

                        // Check to see if the current displayed value is valid regarding its validator
                        PropertyValidatorBase validator = _ownerPropertyEnum.Property.Value.Validator;
                        if (validator != null)
                        {
                            bool newValueValid = validator.Check(newValue, false);

                            if (!validator.Check(valueToValidate, false))
                            {
                                // If the current value is invalid, let's see if the new one will be valid
                                if (newValueValid == false)
                                {
                                    _currentInvalidPropertyEnum = OwnerPropertyEnumerator;
                                    _edit.SelectAll();
                                    _currentValueValidationResult = PropertyValue.ValueValidationResult.ValidatorFailed;
                                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                                     valueToValidate, _currentValueValidationResult));
                                    return(true);
                                }
                            }
                        }

                        if (newValue != _trackbar.Value)
                        {
                            if (newValue > _trackbar.Maximum)
                            {
                                newValue = _trackbar.Maximum;
                            }
                            else if (newValue < _trackbar.Minimum)
                            {
                                newValue = _trackbar.Minimum;
                            }

                            _trackbar.Value = newValue;

                            Text = newValue.ToString();
                            _edit.SelectAll();
                            if (_realtimeChange)
                            {
                                CommitChanges(false);
                            }

                            return(true);
                        }
                    }
                }
            }

            return(base.ProcessKeyPreview(ref m));
        }
示例#3
0
        protected override bool ValidateValue(object value)
        {
            // Validate the underlying value
            if (Validator != null)
            {
                if (Validator.Check(value, false) == false)
                {
                    return(false);
                }
            }

            if (mOwnerEnumerator == null)
            {
                return(true);
            }

            if ((NoLinkWithChildren == false) && !TypeConverter.GetPropertiesSupported())
            {
                // Validate children if any
                PropertyEnumerator childEnum = mOwnerEnumerator.Children;
                if (childEnum.Count > 0)
                {
                    string masterStr = (value as string);

                    char separator = mOwnerEnumerator.Property.Value.GroupedValueSeparator;
                    while (childEnum != childEnum.RightBound)
                    {
                        PropertyValidatorBase validator = childEnum.Property.Value.Validator;

                        masterStr.TrimStart(null);

                        int count = masterStr.IndexOf(separator);

                        if (count != -1)
                        {
                            if ((validator != null) && (validator.Check(masterStr.Substring(0, count), false) == false))
                            {
                                return(false);
                            }

                            if (count + 2 < masterStr.Length)
                            {
                                masterStr = masterStr.Substring(count + 2); // advance after the separator and the leading space
                            }
                            childEnum.MoveNext();
                        }
                        else
                        {
                            if ((validator != null) && (validator.Check(masterStr, false) == false))
                            {
                                return(false);
                            }

                            break;
                        }
                    }
                }
            }

            // Validate parent if any
            PropertyEnumerator parentEnum = mOwnerEnumerator.Parent;

            if ((parentEnum.Property != null) && (parentEnum.Property.Value != null) &&
                (parentEnum.Property.Value.NoLinkWithChildren == false))
            {
                string             str       = "";
                PropertyEnumerator childEnum = parentEnum.Children;
                char separator = parentEnum.Property.Value.GroupedValueSeparator;
                while (childEnum != childEnum.RightBound)
                {
                    if (childEnum.Property.Value == this)
                    {
                        str += (value as string);
                    }
                    else
                    {
                        str += childEnum.Property.Value.GetStringValue();
                    }
                    childEnum.MoveNext();

                    if (childEnum != childEnum.RightBound)
                    {
                        str += separator;
                        str += " ";
                    }
                }

                PropertyValidatorBase validator = parentEnum.Property.Value.Validator;
                if ((validator != null) && (validator.Check(str, false) == false))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#4
0
        public override bool ValidateValue(object value, out PropertyEnumerator invalidPropertyEnum)
        {
            invalidPropertyEnum = Grid.RightBound;

            // Validate the underlying value
            if (Validator != null)
            {
                if (Validator.Check(value, false) == false)
                {
                    invalidPropertyEnum = OwnerEnumerator;
                    return(false);
                }
            }

            if (mOwnerEnumerator == null)
            {
                return(true);
            }

            if ((NoLinkWithChildren == false) && !TypeConverter.GetPropertiesSupported())
            {
                // Validate children if any
                PropertyEnumerator childEnum = mOwnerEnumerator.Children;
                if (childEnum.Count > 0)
                {
                    string masterStr = (value as string);

                    char separator = mOwnerEnumerator.Property.Value.GroupedValueSeparator;
                    while (childEnum != childEnum.RightBound)
                    {
                        PropertyValidatorBase validator = childEnum.Property.Value.Validator;

                        masterStr.TrimStart(null);

                        int count = masterStr.IndexOf(separator);

                        if (count != -1)
                        {
                            if (validator != null)
                            {
                                object valueToCheck = childEnum.Property.Value.TypeConverter.ConvertFrom(
                                    GetTypeDescriptorContext(childEnum),
                                    childEnum.Property.Value.CultureInfo,
                                    masterStr.Substring(0, count));

                                if (validator.Check(valueToCheck, false) == false)
                                {
                                    invalidPropertyEnum = childEnum;
                                    return(false);
                                }
                            }

                            if (count + 2 < masterStr.Length)
                            {
                                masterStr = masterStr.Substring(count + 2); // advance after the separator and the leading space
                            }
                            childEnum.MoveNext();
                        }
                        else
                        {
                            if (validator != null)
                            {
                                object valueToCheck = childEnum.Property.Value.TypeConverter.ConvertFrom(
                                    GetTypeDescriptorContext(childEnum),
                                    childEnum.Property.Value.CultureInfo,
                                    masterStr);

                                if (validator.Check(valueToCheck, false) == false)
                                {
                                    invalidPropertyEnum = childEnum;
                                    return(false);
                                }
                            }

                            break;
                        }
                    }
                }
            }

            // Validate parent if any
            if (mOwnerEnumerator != null)
            {
                PropertyEnumerator parentEnum = mOwnerEnumerator.Parent;
                if ((parentEnum.Property != null) && (parentEnum.Property.Value != null))
                {
                    bool valid = parentEnum.Property.Value.ValidateSelfValueFromModifiedChild(mOwnerEnumerator, value);
                    if (!valid)
                    {
                        invalidPropertyEnum = parentEnum;
                    }
                    return(valid);
                }
            }

/*
 *          PropertyEnumerator parentEnum = mOwnerEnumerator.Parent;
 *          if ((parentEnum.Property != null) && (parentEnum.Property.Value != null) &&
 *              (parentEnum.Property.Value.NoLinkWithChildren == false))
 *          {
 *              string str = "";
 *              PropertyEnumerator childEnum = parentEnum.Children;
 *              char separator = parentEnum.Property.Value.GroupedValueSeparator;
 *              while (childEnum != childEnum.RightBound)
 *              {
 *                  if (childEnum.Property.Value == this)
 *                      str += (value as string);
 *                  else
 *                      str += childEnum.Property.Value.GetStringValue();
 *                  childEnum.MoveNext();
 *
 *                  if (childEnum != childEnum.RightBound)
 *                  {
 *                      str += separator;
 *                      str += " ";
 *                  }
 *              }
 *
 *              PropertyValidatorBase validator = parentEnum.Property.Value.Validator;
 *              if ((validator != null) && (validator.Check(str, false) == false))
 *                  return false;
 *          }*/

            return(true);
        }
示例#5
0
        private void HandleUpDownButton(ButtonID buttonId, bool rotate)
        {
            string strValue = Text;

            PropertyUpDownEventArgs args = new PropertyUpDownEventArgs(
                _ownerPropertyEnum, (buttonId == ButtonID.Up ? PropertyUpDownEventArgs.UpDownButtons.Up :
                                     PropertyUpDownEventArgs.UpDownButtons.Down));

            args.Value = Text;
            string oldStr = Text;

            _ownerPropertyEnum.Property.ParentGrid.OnPropertyUpDown(args);

            if (Text != args.Value)
            {
                strValue = args.Value;
            }
            else
            {
                string[] displayedStrings = _ownerPropertyEnum.Property.Value.GetDisplayedValues();

                if (displayedStrings.Length > 0)
                {
                    strValue = Text;

                    if (_ownerPropertyEnum.Property.Value.HasMultipleValues)
                    {
                        int index = -1;
                        if (buttonId == ButtonID.Down)
                        {
                            index = displayedStrings.Length - 1;
                        }
                        else if (buttonId == ButtonID.Up)
                        {
                            index = 0;
                        }

                        if (index != -1)
                        {
                            strValue = displayedStrings[index];
                        }
                    }
                    else
                    {
                        for (int i = 0; i < displayedStrings.Length; i++)
                        {
                            if (displayedStrings[i] == strValue)
                            {
                                int index = -1;
                                if ((buttonId == ButtonID.Down) && (i > 0))
                                {
                                    index = i - 1;
                                }
                                else if ((buttonId == ButtonID.Down) && rotate)
                                {
                                    index = displayedStrings.Length - 1;
                                }
                                else if ((buttonId == ButtonID.Up) && (i < displayedStrings.Length - 1))
                                {
                                    index = i + 1;
                                }
                                else if ((buttonId == ButtonID.Up) && rotate)
                                {
                                    index = 0;
                                }

                                if (index != -1)
                                {
                                    strValue = displayedStrings[index];
                                }

                                break;
                            }
                        }
                    }
                }
                else if (_ownerPropertyEnum.Property.Value.UnderlyingType != typeof(string))
                {
                    TypeConverter tc = _ownerPropertyEnum.Property.Value.TypeConverter;
                    PropertyTypeDescriptorContext context = _ownerPropertyEnum.Property.Value.GetTypeDescriptorContext(_ownerPropertyEnum);

                    decimal value;
                    object  originalValue;
                    try
                    {
                        if (_ownerPropertyEnum.Property.Value.HasMultipleValues)
                        {
                            DefaultValueAttribute attr = (DefaultValueAttribute)_ownerPropertyEnum.Property.Value.GetAttribute(typeof(DefaultValueAttribute));
                            if (attr != null)
                            {
                                originalValue = attr.Value;
                            }
                            else
                            {
                                originalValue = 0;
                            }
                            value = Convert.ToDecimal(originalValue);
                        }
                        else
                        {
                            originalValue = tc.ConvertFromString(context,
                                                                 _ownerPropertyEnum.Property.Value.CultureInfo, Text);
                            value = Convert.ToDecimal(originalValue);
                        }
                    }
                    catch (Exception e)
                    {
                        _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                        _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text,
                                                         PropertyValue.ValueValidationResult.TypeConverterFailed, e));
                        return;
                    }

                    if (buttonId == ButtonID.Up)
                    {
                        try
                        {
                            value += Increment;
                        }
                        catch (OverflowException)
                        {
                        }
                    }
                    else if (buttonId == ButtonID.Down)
                    {
                        try
                        {
                            value -= Increment;
                        }
                        catch (OverflowException)
                        {
                        }
                    }

                    CultureInfo culture = _ownerPropertyEnum.Property.Value.CultureInfo;
                    strValue = tc.ConvertToString(context, culture,
                                                  Convert.ChangeType(value, originalValue.GetType(), culture));
                    if (strValue == null)
                    {
                        strValue = "";
                    }
                }
            }

            if (Text != strValue)
            {
                _ownerPropertyEnum.Property.Value.PreviousValue = _ownerPropertyEnum.Property.Value.GetValue();

                PropertyValidatorBase validator = _ownerPropertyEnum.Property.Value.Validator;

                // Check current value
                try
                {
                    object valueToValidate = _ownerPropertyEnum.Property.Value.GetValueToValidate(Text);
                    if (validator != null)
                    {
                        if (!validator.Check(_ownerPropertyEnum.Property.Value.GetValueToValidate(Text), false))
                        {
                            if (_edit != null)
                            {
                                _edit.SelectAll();
                            }

                            _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                            _currentValueValidationResult = PropertyValue.ValueValidationResult.ValidatorFailed;
                            _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, OwnerPropertyEnumerator,
                                                             Text, _currentValueValidationResult));
                            return;
                        }
                    }
                }
                catch (Exception e)
                {
                    if (_edit != null)
                    {
                        _edit.SelectAll();
                    }

                    _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text,
                                                     PropertyValue.ValueValidationResult.TypeConverterFailed, e));
                    return;
                }

                if (validator != null)
                {
                    if (!validator.Check(_ownerPropertyEnum.Property.Value.GetValueToValidate(strValue), false))
                    {
                        return;
                    }
                }

                if (_realtimeChange)
                {
                    CommitChanges(strValue, false);
                }
                else
                {
                    Text = strValue;
                }
            }

            if ((_edit != null) && _edit.Focused)
            {
                _edit.SelectAll();
            }
            else
            {
                Invalidate();
            }
        }
 public PropertyValidatorAttribute(Type validatorType, params object[] parameters)
 {
     _validator = (Activator.CreateInstance(validatorType, parameters) as PropertyValidatorBase);
 }
		public PropertyValidatorAttribute(Type validatorType)
		{
			_validator = (Activator.CreateInstance(validatorType) as PropertyValidatorBase);
		}