public void DoubleValidator_IsValid_NullPropertyName()
        {
            DoubleValidator validator = new DoubleValidator(1, true, 10, true);

            string errorMessage;

            Assert.Throws <ArgumentException>(() => validator.IsValid(2, null, null, out errorMessage));

            Assert.Throws <ArgumentException>(() => validator.IsValid(2, String.Empty, null, out errorMessage));
        }
        public void DoubleValidator_IsValid_EqualMinMax()
        {
            DoubleValidator validator = new DoubleValidator(1, true, 1, true);
            bool            result    = validator.IsValid(2, "Property", null, out _);

            Assert.False(result);
        }
        public void DoubleValidator_IsValid_WrongType()
        {
            DoubleValidator validator = new DoubleValidator(1, true, 10, true);
            bool            result    = validator.IsValid("x", "Property", null, out _);

            Assert.False(result);
        }
        /// <summary>
        /// Verifies the input.
        /// </summary>
        /// <param name="sender">Input TextBox.</param>
        /// <param name="e">TextCompositionEventArgs</param>
        private static void OnPreviewInputText(object sender, TextCompositionEventArgs e)
        {
            var textBox = (TextBox)sender;
            // If text box contains selected text we should replace it with e.Text
            string fullText = textBox.SelectionLength > 0
           ? textBox.Text.Replace(textBox.SelectedText, e.Text)
           : textBox.Text.Insert(textBox.CaretIndex, e.Text);

            e.Handled = !DoubleValidator.IsValid(fullText);
        }
Beispiel #5
0
        public static Int64 ConvertToInt64(object objValue, params Int64[] lstDefautValue)
        {
            Int64 nDefault = 0;

            if (lstDefautValue.Length > 0)
            {
                nDefault = lstDefautValue[0];
            }

            if (objValue == System.DBNull.Value)
            {
                return(nDefault);
            }

            string strValue = Convert.ToString(objValue);

            strValue = strValue.Replace("+", "");

            DoubleValidator val = new DoubleValidator();

            val.IsCanNull = false;
            if (val.IsValid(strValue) == false)
            {
                return(nDefault);
            }

            Int64 nRight = 0;
            Int64 nValue = 0;

            if (strValue.IndexOf(".") > 0)
            {
                nRight = Convert.ToInt64(Math.Round(Convert.ToDouble(strValue.Substring(strValue.IndexOf(".") - 1)), 0)); // 四舍六入五成双
                nValue = Convert.ToInt64(strValue.Substring(0, strValue.IndexOf(".") - 1) + "0") + nRight;
            }
            else
            {
                nValue = Convert.ToInt64(strValue);
            }
            return(nValue);
        }
Beispiel #6
0
        public static int ConvertToInt32(object objValue, params int[] lstDefautValue)
        {
            int nDefault = 0;

            if (lstDefautValue.Length > 0)
            {
                nDefault = lstDefautValue[0];
            }

            if (objValue == System.DBNull.Value || objValue == null)
            {
                return(nDefault);
            }

            string strValue = Convert.ToString(objValue);

            strValue = strValue.Replace("+", "");

            DoubleValidator val = new DoubleValidator();

            val.IsCanNull = false;
            if (val.IsValid(strValue) == false)
            {
                return(nDefault);
            }

            double dValue = Math.Round(Convert.ToDouble(strValue), 0); // 四舍六入五成双
            Int64  nLong  = Convert.ToInt64(dValue);
            int    nRet   = nDefault;

            if (nLong > Int32.MaxValue)
            {
                nRet = Convert.ToInt32(Int32.MaxValue - nLong);
            }
            else
            {
                nRet = Convert.ToInt32(nLong);
            }
            return(nRet);
        }
        public void DoubleValidator_IsValid()
        {
            double          min            = 50;
            bool            isMinInclusive = true;
            double          max            = 100;
            bool            isMaxInclusive = true;
            DoubleValidator validator      = new DoubleValidator(min, isMinInclusive, max, isMaxInclusive);
            bool            isValid        = validator.IsValid(49, "foo", null, out _);

            Assert.False(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(49.999, "foo", null, out _);
            Assert.False(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(50, "foo", null, out _);
            Assert.True(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(75, "foo", null, out _);
            Assert.True(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(100, "foo", null, out _);
            Assert.True(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(100.0000001, "foo", null, out _);
            Assert.False(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(101, "foo", null, out _);
            Assert.False(isValid, "IsValid returned incorrect value.");

            PrivateObject accessor = new PrivateObject(validator);

            accessor.SetField("isMinValueInclusive", false);
            isValid = validator.IsValid(50, "foo", null, out _);
            Assert.False(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(50.00001, "foo", null, out _);
            Assert.True(isValid, "IsValid returned incorrect value.");

            accessor.SetField("isMaxValueInclusive", false);
            isValid = validator.IsValid(100, "foo", null, out _);
            Assert.False(isValid, "IsValid returned incorrect value.");
            isValid = validator.IsValid(99.99999, "foo", null, out _);
            Assert.True(isValid, "IsValid returned incorrect value.");
        }