NumOnlyTextBox.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace UAS_LabelMachine.CustomControl
  5. {
  6. public partial class NumOnlyTextBox : TextBox
  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. private void NumOnlyTextBox_Leave(object sender, EventArgs e)
  34. {
  35. this.BackColor = Color.White;
  36. }
  37. private void NumOnlyTextBox_Enter(object sender, EventArgs e)
  38. {
  39. this.BackColor = Color.GreenYellow;
  40. }
  41. }
  42. }