Beispiel #1
0
 public static void SetLastSelectionChangedParams(TextBox element, SelectionChangedParams value)
 {
     element.SetValue(LastSelectionChangedParamsProperty, value);
 }
Beispiel #2
0
        private static void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var obj = sender as TextBox;

            string text        = obj.Text;
            string parsingText = text;
            string correctText = GetCorrectText(obj);

            int?   minInt = null;
            int?   maxInt = null;
            double?minDbl = null;
            double?maxDbl = null;

            NumericInputType inputType      = GetInputType(obj);
            CultureInfo      currentCulture = CultureInfo.CurrentUICulture;

            string ignoredSymbol    = GetIgnoredSymbol(obj);
            bool   hasIgnoredSymbol = false;

            if (!string.IsNullOrWhiteSpace(ignoredSymbol))
            {
                int indx = text.LastIndexOf(ignoredSymbol);
                if (indx != -1)
                {
                    hasIgnoredSymbol = true;
                    parsingText      = text.Substring(0, indx);
                }
                else
                {
                    indx             = correctText.LastIndexOf(ignoredSymbol);
                    hasIgnoredSymbol = (indx != -1);
                }
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                switch (inputType)
                {
                case NumericInputType.Integer:
                    minInt = ParseInt(GetMinValue(obj), NumberStyles.None, currentCulture);

                    parsingText = (minInt ?? default(int)).ToString();
                    break;

                case NumericInputType.Double:
                    minDbl = double.Parse(GetMinValue(obj), NumberStyles.AllowDecimalPoint, currentCulture);

                    parsingText = (minDbl ?? default(double)).ToString();
                    break;

                default:
                    hasIgnoredSymbol = false;
                    parsingText      = correctText;
                    break;
                }

                text = $"{parsingText}{((hasIgnoredSymbol) ? ignoredSymbol : "")}";

                obj.Text = text;
                obj.SelectAll();
                return;
            }

            if (text == correctText)
            {
                return;
            }

            bool isCorrect    = true;
            bool valueChanged = false;

            switch (inputType)
            {
            case NumericInputType.Integer:
                isCorrect = int.TryParse(parsingText, NumberStyles.None, currentCulture, out int intResult);

                minInt = ParseInt(GetMinValue(obj), NumberStyles.None, currentCulture);
                maxInt = ParseInt(GetMaxValue(obj), NumberStyles.None, currentCulture);

                if (minInt != null && intResult < minInt)
                {
                    intResult    = minInt.Value;
                    valueChanged = true;
                }

                if (maxInt != null && intResult > maxInt)
                {
                    intResult    = maxInt.Value;
                    valueChanged = true;
                }

                if (valueChanged)
                {
                    parsingText = intResult.ToString();
                }

                break;

            case NumericInputType.Double:
                parsingText = ValidateDecimalPoint(parsingText, currentCulture);
                isCorrect   = double.TryParse(parsingText, NumberStyles.AllowDecimalPoint, currentCulture, out double dblResult);

                minDbl = double.Parse(GetMinValue(obj), NumberStyles.AllowDecimalPoint, currentCulture);
                maxDbl = double.Parse(GetMaxValue(obj), NumberStyles.AllowDecimalPoint, currentCulture);

                if (minDbl != null && dblResult < minDbl)
                {
                    dblResult    = minDbl.Value;
                    valueChanged = true;
                }

                if (maxDbl != null && dblResult > maxDbl)
                {
                    dblResult    = maxDbl.Value;
                    valueChanged = true;
                }

                if (valueChanged)
                {
                    parsingText = dblResult.ToString();
                }

                break;

            default:
                break;
            }

            if (isCorrect)
            {
                text = $"{parsingText}{((hasIgnoredSymbol) ? ignoredSymbol : "")}";
                SetCorrectText(obj, text);

                if (valueChanged)
                {
                    obj.SelectionChanged -= OnSelectionChanged;
                    obj.TextChanged      -= OnTextChanged;

                    obj.Text           = text;
                    obj.SelectionStart = parsingText.Length;

                    obj.SelectionChanged += OnSelectionChanged;
                    obj.TextChanged      += OnTextChanged;
                }
            }
            else
            {
                SelectionChangedParams selectionParams = Services.GetLastSelectionChangedParams(obj);
                bool oldTextEmpty = string.IsNullOrWhiteSpace(correctText);

                obj.SelectionChanged -= OnSelectionChanged;
                obj.TextChanged      -= OnTextChanged;

                obj.Text            = (oldTextEmpty) ? "" : correctText;
                obj.SelectionStart  = selectionParams.Start;
                obj.SelectionLength = selectionParams.Length;

                obj.TextChanged      += OnTextChanged;
                obj.SelectionChanged += OnSelectionChanged;
            }

            Services.SetLastSelectionChangedParams(obj, new SelectionChangedParams(obj.SelectionStart, obj.SelectionLength));
            Services.SetIgnoreSelectionChanged(obj, false);
        }