private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e) { try { this.ToolTip = null; TextBoxNumber tb = sender as TextBoxNumber; bool bInput = Regex.IsMatch(e.Text, @"[0-9.+-]"); if (!bInput) { e.Handled = true; ShowToolTip($"输入的当前值{e.Text}格式不正确!"); } else { string value = tb.Text + e.Text; if (tb.MinValue >= 0 && e.Text == "-") { e.Handled = true; ShowToolTip($"输入的当前值{value}不能小于最小值{tb.MinValue}"); } if (Convert.ToDouble(value) > tb.MaxValue) { e.Handled = true; ShowToolTip($"输入的当前值{value}不能大于最大值{tb.MaxValue}"); } //if (Convert.ToDouble(value) < tb.MinValue) //{ // e.Handled = true; //} } } catch { e.Handled = true; } }
private void TextBoxNumber_LostFocus(object sender, RoutedEventArgs e) { TextBoxNumber tb = sender as TextBoxNumber; string value = tb.Text; toolTip.IsOpen = false; this.ToolTip = null; try { if (Convert.ToDouble(value) < tb.MinValue) { tb.Text = tb.MinValue.ToString(); ShowToolTip($"输入的当前值{value}不能大于最大值{tb.MaxValue}"); } if (Convert.ToDouble(value) > tb.MaxValue) { tb.Text = tb.MaxValue.ToString(); ShowToolTip($"输入的当前值{value}不能大于最大值{tb.MaxValue}"); } } catch { tb.Text = ""; } //if (!isValue) //{ // MessageBox.Show(tb.Tag + "数据不正确!" , "数据配置提示", MessageBoxButton.OK, MessageBoxImage.Error); // //tb.Focus(); // //tb.SelectionStart = tb.Text.Length; //} }