/// <summary> /// /// </summary> /// <param name="state"></param> /// <returns></returns> protected override object GetEditValue(MaskManagerState state) { if (((NumericMaskManagerState)state).IsNull) { return(null); } string editText = GetEditText(state); if (_formatters[0]._Is100Multiplied) { editText = NumericMaskLogic.Div100(editText); } if (editText.IndexOf('.') >= 0) { while (editText.EndsWith("0")) { editText = editText.Substring(0, editText.Length - 1); } } if (editText.EndsWith(".")) { editText = editText.Substring(0, editText.Length - 1); } if ((editText.Length == 0) || (editText == "-")) { editText = editText + '0'; } if (_hasValueType) { try { return(Convert.ChangeType(editText, _editValueTypeCode)); } catch { goto Label_00E6; } } try { return(Convert.ChangeType(editText, typeof(Int32))); } catch { } try { return(Convert.ChangeType(editText, typeof(Decimal))); } catch { } Label_00E6: return(null); }
/// <summary> /// 数字型掩码管理者 /// </summary> /// <param name="formatString">掩码表达式</param> /// <param name="managerCultureInfo">区域信息</param> /// <param name="allowNull">是否允许空</param> public NumericMaskManager(string formatString, CultureInfo managerCultureInfo, bool allowNull) : base(NumericMaskManagerState.NullInstance) { _formatters = new NumericFormatter[2]; _AllowNull = allowNull; formatString = NumericFormatter.Expand(formatString, managerCultureInfo); _negativeSignString = managerCultureInfo.NumberFormat.NegativeSign; int index = formatString.Replace(@"\\", "//").Replace(@"\;", "/:").IndexOf(';'); if (index < 0) { _formatters[0] = new NumericFormatter(formatString, managerCultureInfo); _formatters[1] = null; } else { _formatters[0] = new NumericFormatter(formatString.Substring(0, index), managerCultureInfo); _formatters[1] = new NumericFormatter(formatString.Substring(index + 1), managerCultureInfo); if (_formatters[0].MaxDigitsBeforeDecimalSeparator != _formatters[1].MaxDigitsBeforeDecimalSeparator) { throw new ArgumentException("Incorrect mask: the max number of digits before the decimal separator in the positive and negative patterns must match"); } if (_formatters[0].MaxDigitsAfterDecimalSeparator != _formatters[1].MaxDigitsAfterDecimalSeparator) { throw new ArgumentException("Incorrect mask: the max number of digits after the decimal separator in the positive and negative patterns must match"); } if (_formatters[0].MinDigitsBeforeDecimalSeparator != _formatters[1].MinDigitsBeforeDecimalSeparator) { throw new ArgumentException("Incorrect mask: the min number of digits before the decimal separator in the positive and negative patterns must match"); } if (_formatters[0].MinDigitsAfterDecimalSeparator != _formatters[1].MinDigitsAfterDecimalSeparator) { throw new ArgumentException("Incorrect mask: the min number of digits after the decimal separator in the positive and negative patterns must match"); } if (_formatters[0]._Is100Multiplied != _formatters[1]._Is100Multiplied) { throw new ArgumentException("Incorrect mask: the percent type (% or %%) in the positive and negative patterns must match"); } } _logic = new NumericMaskLogic(_formatters[0].MaxDigitsBeforeDecimalSeparator, _formatters[0].MinDigitsBeforeDecimalSeparator, _formatters[0].MinDigitsAfterDecimalSeparator, _formatters[0].MaxDigitsAfterDecimalSeparator, managerCultureInfo); if ((_formatters[0].MaxDigitsAfterDecimalSeparator > 0) || _formatters[0]._Is100Multiplied) { _editValueTypeCode = typeof(Decimal); } }
/// <summary> /// 设置初始值 /// </summary> /// <param name="initialEditValue"></param> public override void SetInitialEditValue(object initialEditValue) { if (initialEditValue == null) { SetInitialEditText(null); } else { // hdt ForceEditValueTypeCode(initialEditValue.GetType()); int maxDigitsAfterDecimalSeparator = _formatters[0].MaxDigitsAfterDecimalSeparator; if (_formatters[0]._Is100Multiplied) { maxDigitsAfterDecimalSeparator += 2; } string format = "{0:f" + maxDigitsAfterDecimalSeparator.ToString(CultureInfo.InvariantCulture) + "}"; string input = string.Format(CultureInfo.InvariantCulture, format, new object[] { initialEditValue }); if (_formatters[0]._Is100Multiplied) { input = NumericMaskLogic.Mul100(input); } SetInitialEditText(input); } }