private static void OnInputTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var obj = d as TextBox; if (obj == null) { return; } NumericInputType oldValue = (NumericInputType)e.OldValue; if (oldValue == NumericInputType.None) { obj.TextChanging += OnTextChanging; obj.TextChanged += OnTextChanged; obj.SelectionChanged += OnSelectionChanged; obj.GotFocus += OnGotFocus; obj.LostFocus += OnLostFocus; } NumericInputType newValue = (NumericInputType)e.NewValue; if (newValue == NumericInputType.None) { obj.TextChanging -= OnTextChanging; obj.TextChanged -= OnTextChanged; obj.SelectionChanged -= OnSelectionChanged; obj.GotFocus -= OnGotFocus; obj.LostFocus -= OnLostFocus; } }
private static void OnLostFocus(object sender, RoutedEventArgs e) { var obj = sender as TextBox; CultureInfo currentCulture = CultureInfo.CurrentUICulture; NumericInputType inputType = GetInputType(obj); if (inputType == NumericInputType.Double) { double?val = ParseDouble(GetCorrectText(obj), NumberStyles.AllowDecimalPoint, currentCulture); if (val != null) { SetCorrectText(obj, val.Value.ToString("G", currentCulture)); } } obj.Text = GetCorrectText(obj); }
public static void SetNumericInputType(NumericUpDown control, NumericInputType inputType) { control.SetValue(NumericInputTypeProperty, inputType); }
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); }
public static void SetInputType(TextBox element, NumericInputType value) { element.SetValue(InputTypeProperty, value); }