UserTextBox.cs 992 B

1234567891011121314151617181920212223242526272829303132
  1. using System.Windows.Forms;
  2. namespace PaintDotNet.CustomControl
  3. {
  4. public partial class UserTextBox : TextBox
  5. {
  6. /// <summary>
  7. /// 允许输入正整数的textbox
  8. /// 如有其它需要,可以再此基础上扩展或者独立创建也可以
  9. /// </summary>
  10. public UserTextBox()
  11. {
  12. this.KeyPress += new KeyPressEventHandler(TextBoxFoucs_KeyPress);
  13. }
  14. private void TextBoxFoucs_KeyPress(object sender, KeyPressEventArgs e)
  15. {
  16. if (e.KeyChar != '\b')//这是允许输入退格键 
  17.             {
  18. int len = this.Text.Length;
  19. if (len < 1 && e.KeyChar == '0')
  20. {
  21. e.Handled = true;
  22. }
  23. else if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 
  24.                 {
  25. e.Handled = true;
  26. }
  27. }
  28. }
  29. }
  30. }