Esempio n. 1
0
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult result;
            string           val = (string)value;

            #region check whether empty or non-numeric
            if (string.IsNullOrEmpty(val))
            {
                return(new ValidationResult(true, ""));
            }

            if (Nonnumeric.IsMatch(val))
            {
                return(new ValidationResult(true, ""));
            }
            #endregion

            Boolean thousandsValid = false;
            try
            {
                Double.Parse(val, ThousandStyles);
                if (!(Settings.Default.SifUseThousandsSeparator || ignoreLocal))
                {
                    thousandsValid = true;
                    Double.Parse(val, NormalStyles);
                }
                result = new ValidationResult(true, "");
            }
            catch (FormatException e)
            {
                if (thousandsValid && !ignoreLocal)
                {
                    var          messageBox = new NumberValidationMessageBox();
                    DialogResult userChoice = messageBox.ShowDialog();
                    switch (userChoice)
                    {
                    case (DialogResult.Yes):
                        ignoreLocal = true;
                        break;

                    case (DialogResult.Ignore):
                        Settings.Default.SifUseThousandsSeparator = true;
                        break;
                    }

                    // get the validation result without asking infinite times
                    bool old = ignoreLocal;
                    ignoreLocal = true;
                    result      = Validate(value, cultureInfo);
                    ignoreLocal = old;
                }
                else
                {
                    result = new ValidationResult(false, e.Message);
                }
            }

            return(result);
        }
Esempio n. 2
0
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult result;
            string val = (string) value;

            #region check whether empty or non-numeric
            if (string.IsNullOrEmpty(val))
            {
                return new ValidationResult(true, "");
            }

            if (Nonnumeric.IsMatch(val))
            {
                return  new ValidationResult(true, "");
            }
            #endregion

            Boolean thousandsValid = false;
            try
            {
                Double.Parse(val, ThousandStyles);
                if (!(Settings.Default.SifUseThousandsSeparator || ignoreLocal))
                {
                    thousandsValid = true;
                    Double.Parse(val, NormalStyles);
                }
                result = new ValidationResult(true, "");
            }
            catch (FormatException e)
            {
                if (thousandsValid && !ignoreLocal)
                {
                    var messageBox = new NumberValidationMessageBox();
                    DialogResult userChoice = messageBox.ShowDialog();
                    switch (userChoice)
                    {
                        case (DialogResult.Yes) :
                            ignoreLocal = true;
                            break;
                        case (DialogResult.Ignore) :
                            Settings.Default.SifUseThousandsSeparator = true;
                            break;
                    }

                    // get the validation result without asking infinite times
                    bool old = ignoreLocal;
                    ignoreLocal = true;
                    result = Validate(value, cultureInfo);
                    ignoreLocal = old;
                } 
                else 
                {
                    result = new ValidationResult(false, e.Message);
                }
            }

            return result;
        }