using System.Windows.Forms; namespace PaintDotNet.CustomControl { public partial class UserTextBox : TextBox { /// /// 允许输入正整数的textbox /// 如有其它需要,可以再此基础上扩展或者独立创建也可以 /// public UserTextBox() { this.KeyPress += new KeyPressEventHandler(TextBoxFoucs_KeyPress); } private void TextBoxFoucs_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != '\b')//这是允许输入退格键              { int len = this.Text.Length; if (len < 1 && e.KeyChar == '0') { e.Handled = true; } else if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字                  { e.Handled = true; } } } } }