using System; using System.Windows; namespace CommonControlLibrary { public partial class NumericTextbox : System.Windows.Controls.TextBox { public NumericTextbox() : base() { this.AddHandler(System.Windows.DataObject.PastingEvent, new System.Windows.DataObjectPastingEventHandler(this.TextBoxPasting)); } protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e) { base.OnPreviewTextInput(e); e.Handled = !IsTextAllowed(e.Text); } private bool IsTextAllowed(string text) { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[^0-9]+"); return !regex.IsMatch(text); } private void TextBoxPasting(object sender, System.Windows.DataObjectPastingEventArgs e) { if (e.DataObject.GetDataPresent(typeof(System.String))) { System.String text = (System.String)e.DataObject.GetData(typeof(System.String)); if (!IsTextAllowed(text)) e.CancelCommand(); } else e.CancelCommand(); } } }
typeof(System.String)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)