using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace ClipboardTextBoxExample { public partial class ClipboardTextBox : TextBox { private const int WM_PASTE = 0x0302; public delegate void ClipboardEventHandler(object sender, ClipboardEventArgs e); [Category("Clipboard")] public event ClipboardEventHandler PastedText; public ClipboardTextBox() { InitializeComponent(); } protected override void WndProc(ref Message m) { if (m.Msg == WM_PASTE) { ClipboardEventArgs e = new ClipboardEventArgs(Clipboard.GetText()); if (PastedText != null) { string strippedText = ""; for (int i = 0; i < Clipboard.GetText().Length; i++) { if ((char.IsDigit(Clipboard.GetText()[i])) && ((Clipboard.GetText()[i]== '0') || (Clipboard.GetText()[i]=='1'))) strippedText += Clipboard.GetText()[i].ToString(); } if (strippedText != Clipboard.GetText()) { e.Supress = true; } //There were non-numbers in the pasted text else { e.Supress = false; } PastedText(this, e); } if (!e.Supress) base.WndProc(ref m); //only pass message along if not suppressing } else base.WndProc(ref m); } } public class ClipboardEventArgs : EventArgs { public string ClipboardText { get; set; } public bool Supress { get; set; } public ClipboardEventArgs(string clipboardText) { this.ClipboardText = clipboardText; } } }