示例#1
0
        /// <summary>
        /// The try extrapolate.
        /// </summary>
        /// <param name="cell">
        /// The cell.
        /// </param>
        /// <param name="p1">
        /// The p 1.
        /// </param>
        /// <param name="p2">
        /// The p 2.
        /// </param>
        /// <param name="result">
        /// The result.
        /// </param>
        /// <returns>
        /// The try extrapolate.
        /// </returns>
        private bool TryExtrapolate(CellRef cell, CellRef p1, CellRef p2, out object result)
        {
            try
            {
                var v1 = this.GetCellValue(p1);
                var v2 = this.GetCellValue(p2);

                try
                {
                    double d1 = Convert.ToDouble(v1);
                    double d2 = Convert.ToDouble(v2);
                    v1 = d1;
                    v2 = d2;
                }
                catch (Exception)
                {
                }

                int    deltaColumns = p2.Column - p1.Column;
                int    deltaRows    = p2.Row - p1.Row;
                double f            = 0;
                if (cell.Column < p1.Column || cell.Column > p2.Column)
                {
                    if (deltaColumns > 0)
                    {
                        f = 1.0 * (cell.Column - p1.Column) / deltaColumns;
                    }
                }

                if (cell.Row < p1.Row || cell.Row > p2.Row)
                {
                    if (deltaRows > 0)
                    {
                        f = 1.0 * (cell.Row - p1.Row) / deltaRows;
                    }
                }

                if (f == 0)
                {
                    result = v1;
                    return(true);
                }

                object tmp1, tmp2, tmp3;
                ReflectionMath.TrySubtract(v2, v1, out tmp1);
                ReflectionMath.TryMultiply(tmp1, f, out tmp2);
                ReflectionMath.TryAdd(v1, tmp2, out tmp3);
                result = tmp3;
                return(true);
            }
            catch
            {
                result = null;
                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// Changes the value.
        /// </summary>
        /// <param name="sign">The sign of the change.</param>
        /// <param name="isLargeChange">The is Large Change.</param>
        private void ChangeValue(int sign, bool isLargeChange)
        {
            var change = isLargeChange ? this.LargeChange : this.SmallChange;

            if (change == null)
            {
                return;
            }

            if (change is string)
            {
                object c;
                var    type = this.Value.GetType();
                if (ReflectionMath.TryParse(type, (string)change, CultureInfo.InvariantCulture, out c))
                {
                    object result;
                    if (sign > 0)
                    {
                        if (ReflectionMath.TryAdd(this.Value, c, out result))
                        {
                            this.Value = result;
                            return;
                        }
                    }
                    else
                    {
                        if (ReflectionMath.TrySubtract(this.Value, c, out result))
                        {
                            this.Value = result;
                            //this.Value = c.Convert(this.CoerceSpinnerValue(result), typeof(string), null, this.Culture);
                            return;
                        }
                    }
                }
            }
            if (this.Value != null)
            {
                ITypeDescriptorContext context = null;
                var c = TypeDescriptor.GetConverter(this.Value);
                if (c.CanConvertFrom(null, change.GetType()))
                {
                    var    convertedChange = c.ConvertFrom(context, this.Culture ?? CultureInfo.InvariantCulture, change);
                    object result;
                    if (sign > 0)
                    {
                        if (ReflectionMath.TryAdd(this.Value, convertedChange, out result))
                        {
                            this.Value = this.CoerceSpinnerValue(result);
                            return;
                        }
                    }
                    else
                    {
                        if (ReflectionMath.TrySubtract(this.Value, convertedChange, out result))
                        {
                            this.Value = this.CoerceSpinnerValue(result);
                            return;
                        }
                    }
                }
            }

            if (this.Value is double)
            {
                var doubleChange = Convert.ToDouble(change);
                var currentValue = (double)this.Value;
                var newValue     = sign > 0 ? currentValue + doubleChange : currentValue - doubleChange;
                if (sign > 0 && currentValue > double.MaxValue - doubleChange)
                {
                    newValue = double.MaxValue;
                }

                if (sign < 0 && currentValue < double.MinValue + doubleChange)
                {
                    newValue = double.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);
                return;
            }

            if (this.Value is int)
            {
                var intChange    = Convert.ToInt32(change);
                var currentValue = (int)this.Value;
                var newValue     = sign > 0 ? currentValue + intChange : currentValue - intChange;
                if (sign > 0 && currentValue > int.MaxValue - intChange)
                {
                    newValue = int.MaxValue;
                }

                if (sign < 0 && currentValue < int.MinValue + intChange)
                {
                    newValue = int.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);
                return;
            }

            if (this.Value is long)
            {
                var longChange   = Convert.ToInt64(change);
                var currentValue = (long)this.Value;
                var newValue     = sign > 0 ? currentValue + longChange : currentValue - longChange;
                if (sign > 0 && currentValue > long.MaxValue - longChange)
                {
                    newValue = long.MaxValue;
                }

                if (sign < 0 && currentValue < long.MinValue + longChange)
                {
                    newValue = long.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);
                return;
            }

            if (this.Value is short)
            {
                var shortChange  = Convert.ToInt16(change);
                var currentValue = (short)this.Value;
                var newValue     = (short)(sign > 0 ? currentValue + shortChange : currentValue - shortChange);
                if (sign > 0 && currentValue > short.MaxValue - shortChange)
                {
                    newValue = short.MaxValue;
                }

                if (sign < 0 && currentValue < short.MinValue + shortChange)
                {
                    newValue = short.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);
                return;
            }

            if (this.Value is ulong)
            {
                var intChange    = Convert.ToUInt64(change);
                var currentValue = (ulong)this.Value;
                var newValue     = sign > 0 ? currentValue + intChange : currentValue - intChange;
                if (sign > 0 && currentValue > ulong.MaxValue - intChange)
                {
                    newValue = ulong.MaxValue;
                }

                if (sign < 0 && currentValue < ulong.MinValue + intChange)
                {
                    newValue = ulong.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);

                return;
            }

            if (this.Value is uint)
            {
                var intChange    = Convert.ToUInt32(change);
                var currentValue = (uint)this.Value;
                var newValue     = sign > 0 ? currentValue + intChange : currentValue - intChange;
                if (sign > 0 && currentValue > uint.MaxValue - intChange)
                {
                    newValue = uint.MaxValue;
                }

                if (sign < 0 && currentValue < uint.MinValue + intChange)
                {
                    newValue = uint.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);

                return;
            }

            if (this.Value is ushort)
            {
                var intChange    = Convert.ToUInt16(change);
                var currentValue = (ushort)this.Value;
                var newValue     = (ushort)(sign > 0 ? currentValue + intChange : currentValue - intChange);
                if (sign > 0 && currentValue > ushort.MaxValue - intChange)
                {
                    newValue = ushort.MaxValue;
                }

                if (sign < 0 && currentValue < ushort.MinValue + intChange)
                {
                    newValue = ushort.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);

                return;
            }

            if (this.Value is byte)
            {
                var intChange    = Convert.ToByte(change);
                var currentValue = (byte)this.Value;
                var newValue     = (byte)(sign > 0 ? currentValue + intChange : currentValue - intChange);
                if (sign > 0 && currentValue > byte.MaxValue - intChange)
                {
                    newValue = byte.MaxValue;
                }

                if (sign < 0 && currentValue < byte.MinValue + intChange)
                {
                    newValue = byte.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);

                return;
            }

            if (this.Value is sbyte)
            {
                var intChange    = Convert.ToSByte(change);
                var currentValue = (sbyte)this.Value;
                var newValue     = (sbyte)(sign > 0 ? currentValue + intChange : currentValue - intChange);
                if (sign > 0 && currentValue > sbyte.MaxValue - intChange)
                {
                    newValue = sbyte.MaxValue;
                }

                if (sign < 0 && currentValue < sbyte.MinValue + intChange)
                {
                    newValue = sbyte.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);

                return;
            }

            if (this.Value is float)
            {
                var floatChange  = Convert.ToSingle(change);
                var currentValue = (float)this.Value;
                var newValue     = sign > 0 ? currentValue + floatChange : currentValue - floatChange;
                if (sign > 0 && currentValue > float.MaxValue - floatChange)
                {
                    newValue = float.MaxValue;
                }

                if (sign < 0 && currentValue < float.MinValue + floatChange)
                {
                    newValue = float.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);
                return;
            }

            if (this.Value is decimal)
            {
                var decimalChange = Convert.ToDecimal(change);
                var currentValue  = (decimal)this.Value;
                var newValue      = sign > 0 ? currentValue + decimalChange : currentValue - decimalChange;
                if (sign > 0 && currentValue > decimal.MaxValue - decimalChange)
                {
                    newValue = decimal.MaxValue;
                }

                if (sign < 0 && currentValue < decimal.MinValue + decimalChange)
                {
                    newValue = decimal.MinValue;
                }

                this.Value = this.CoerceSpinnerValue(newValue);
                return;
            }

            if (this.Value is DateTime)
            {
                object newValue = null;
                if (change is TimeSpan)
                {
                    var span = (TimeSpan)change;
                    newValue = ((DateTime)this.Value).AddDays(span.TotalDays * sign);
                }

                if (this.IsNumeric(change))
                {
                    var doubleChange = Convert.ToDouble(change);
                    if (Math.Abs(doubleChange) > double.Epsilon)
                    {
                        newValue = ((DateTime)this.Value).AddDays(doubleChange * sign);
                    }
                }

                this.Value = this.CoerceSpinnerValue(newValue);
                return;
            }

            if (this.Value is TimeSpan)
            {
                var    current  = (TimeSpan)this.Value;
                object newValue = null;
                if (change is TimeSpan)
                {
                    var span = (TimeSpan)change;
                    newValue = sign > 0 ? current.Add(span) : current.Subtract(span);
                }

                if (this.IsNumeric(change))
                {
                    var doubleChange = Convert.ToDouble(change);
                    if (Math.Abs(doubleChange) > double.Epsilon)
                    {
                        newValue = current.Add(TimeSpan.FromSeconds(doubleChange * sign));
                    }
                }

                this.Value = this.CoerceSpinnerValue(newValue);
            }
        }
        /// <summary>
        /// Tries to extrapolate.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <param name="p1">The first source cell.</param>
        /// <param name="p2">The second source cell.</param>
        /// <param name="result">The result.</param>
        /// <returns>
        /// True is extrapolation was successful.
        /// </returns>
        // ReSharper disable once UnusedMethodReturnValue.Local
        private bool TryExtrapolate(CellRef cell, CellRef p1, CellRef p2, out object result)
        {
            try
            {
                var v1 = this.getCellValue(p1);
                var v2 = this.getCellValue(p2);

                if (v1 == null || v2 == null)
                {
                    result = null;
                    return(false);
                }

                int    deltaColumns = p2.Column - p1.Column;
                int    deltaRows    = p2.Row - p1.Row;
                double f            = 0;
                if (cell.Column < p1.Column || cell.Column > p2.Column)
                {
                    if (deltaColumns > 0)
                    {
                        f = 1.0 * (cell.Column - p1.Column) / deltaColumns;
                    }
                }

                if (cell.Row < p1.Row || cell.Row > p2.Row)
                {
                    if (deltaRows > 0)
                    {
                        f = 1.0 * (cell.Row - p1.Row) / deltaRows;
                    }
                }

                if (f.Equals(0))
                {
                    result = v1;
                    return(true);
                }

                object tmp1, tmp2, tmp3;
                ReflectionMath.TrySubtract(v2, v1, out tmp1);
                if (tmp1 == null)
                {
                    result = null;
                    return(false);
                }

                ReflectionMath.TryMultiply(tmp1, f, out tmp2);
                if (tmp2 == null)
                {
                    result = null;
                    return(false);
                }

                ReflectionMath.TryAdd(v1, tmp2, out tmp3);

                result = tmp3;
                return(tmp3 != null);
            }
            catch
            {
                result = null;
                return(false);
            }
        }