Esempio n. 1
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();
        }
Esempio n. 2
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();
            }
        }