NumOnlyTextBox.cs 941 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon
  5. {
  6. public partial class NumOnlyTextBox : EnterTextBox
  7. {
  8. private bool Negative1;
  9. public NumOnlyTextBox()
  10. {
  11. InitializeComponent();
  12. }
  13. //通过标识来判断是否允许输入负数
  14. public bool Negative
  15. {
  16. get
  17. {
  18. return Negative1;
  19. }
  20. set
  21. {
  22. Negative1 = value;
  23. }
  24. }
  25. private void NumOnlyTextBox_KeyPress(object sender, KeyPressEventArgs e)
  26. {
  27. // 允许输入:数字、退格键(8)、全选(1)、复制(3)、粘贴(22)
  28. if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 1 && e.KeyChar != 3 && e.KeyChar != 22)
  29. {
  30. e.Handled = true;
  31. }
  32. }
  33. }
  34. }