从写TextBox
时间:2011-03-29 来源:菜鸟成长日记
从写TextBox 禁止粘贴
using System; using System.Windows.Forms; namespace winTest { public class MyTextBox:System.Windows.Forms.TextBox { public const int WM_PASTE = 0x0302;//粘貼消息 public const int WM_CHAR = 0x0102; /// <summary> /// 禁止粘贴 /// </summary> /// <param name="oMessage"></param> protected override void WndProc(ref Message oMessage) { if (oMessage.Msg == WM_PASTE) { IDataObject paste = Clipboard.GetDataObject(); string num = paste.GetData(typeof(string)).ToString().Trim(); for (int i = 0; i < num.Length; i++) { if (!char.IsNumber(num[i])) { return; } } } else if (oMessage.Msg == WM_CHAR) { Console.WriteLine(oMessage.WParam); if ((int)oMessage.WParam != 8) { if ((int)oMessage.WParam < 48 || (int)oMessage.WParam > 57) { return; } } } base.WndProc(ref oMessage); } } }
相关阅读 更多 +