public static bool IsStringNumeric(string value, NumericProperties np) { bool ok = true; int len = value.Length, i = 0, dp = -1; if (np.AllowSign && value.StartsWith(NumberFormatInfo.CurrentInfo.NegativeSign)) { i++; } for (; i < len; i++) { if (np.AllowDP && dp == -1 && value[i] == '.') { dp = i; } else { ok &= char.IsDigit(value[i]); } } if (ok && dp >= 0) { ok = len - dp - 1 <= np.Precision; } return(ok && len <= np.Length); }
public static void OnFormatChanged(DependencyObject d, NumericProperties np, string format) { np.Parse(format); Size size = UIUtils.MeasureText("".PadRight(np.Length, '9'), (Control)d); ((Control)d).Width = size.Width + (d is NumericTextBox ? 12 : 20); //((NumericTextBox)sender).Width = TextRenderer.MeasureText("".PadRight(Length, '9'), ((NumericTextBox)sender).Font); }
protected override void OnPreviewTextInput(TextCompositionEventArgs e) { TextBox textBox = (TextBox)e.OriginalSource; string text = textBox.SelectionLength > 0 ? textBox.Text.Remove(textBox.SelectionStart, textBox.SelectionLength) : textBox.Text; text = text.Insert(textBox.CaretIndex, e.Text); e.Handled = !NumericProperties.IsStringNumeric(text, np); base.OnPreviewTextInput(e); }
protected override void OnPreviewTextInput(TextCompositionEventArgs e) { TextBox textBox = (TextBox)e.OriginalSource; string text = textBox.SelectionLength > 0 ? textBox.Text.Remove(textBox.SelectionStart, textBox.SelectionLength) : textBox.Text; text = text.Insert(textBox.CaretIndex, e.Text); if (!(e.Handled = !NumericProperties.IsStringNumeric(text, np))) { updateText = false; Value = double.Parse(text == "" || text == "." || text == "-" || text == "-." ? "0" : text, np.Styles, CultureInfo.InvariantCulture); updateText = true; } base.OnPreviewTextInput(e); }
private static void OnFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { NumericProperties.OnFormatChanged(d, ((NumericComboBox)d).np, (string)e.NewValue); }