ucNumberKey.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace MeterModuleLibrary
  11. {
  12. public delegate void EventSetNumKey(object o,string s);
  13. public partial class ucNumberKey : UserControl
  14. {
  15. public ucNumberKey()
  16. {
  17. InitializeComponent();
  18. }
  19. /// <summary>
  20. /// 默认控制进行数字验证
  21. /// </summary>
  22. public bool bControl = true;
  23. public string strValue = "";
  24. /// <summary>
  25. /// 主界面调用填充数值
  26. /// </summary>
  27. public event EventSetNumKey evoice;
  28. public void setTxtNum(string sValue)
  29. {
  30. if (!string.IsNullOrEmpty(sValue))
  31. {
  32. Button btn = new Button();
  33. btn.Text = sValue;
  34. setTxtValue(btn, sValue.Length - 1);
  35. }
  36. }
  37. private void btnValue_Click(object sender, EventArgs e)
  38. {
  39. Button btn = (Button)sender;
  40. if (bControl)
  41. {
  42. switch (btn.Name)
  43. {
  44. case "btnSpot":
  45. if (txtValue.Text.Trim() != "" && !txtValue.Text.Trim().Contains("."))
  46. {
  47. setTxtValue(btn, 0);
  48. }
  49. break;
  50. case "btn00":
  51. if (txtValue.Text.Trim() != "" && (txtValue.Text.Trim().Substring(0, 1) != "0" || txtValue.Text.Trim().Contains(".")))
  52. {
  53. setTxtValue(btn, 1);
  54. }
  55. break;
  56. case "btn0":
  57. if (txtValue.Text.Trim() == "" || txtValue.Text.Trim().Contains(".") || (txtValue.Text.Trim() != "" && txtValue.Text.Substring(0, 1) != "0"))
  58. {
  59. setTxtValue(btn, 0);
  60. }
  61. break;
  62. default:
  63. setTxtValue(btn, 0);
  64. break;
  65. }
  66. }
  67. else
  68. {
  69. int m = 0;
  70. if (btn.Name == "btn00")
  71. {
  72. m = 1;
  73. }
  74. setTxtValue(btn, m);
  75. }
  76. }
  77. private void setTxtValue(Button btn, int m)
  78. {
  79. int i = txtValue.SelectionStart;
  80. txtValue.Focus();
  81. txtValue.Text = txtValue.Text.Trim() + btn.Text;
  82. txtValue.Select(++i + m, 0);
  83. }
  84. private void btnClean_Click(object sender, EventArgs e)
  85. {
  86. txtValue.Text = "";
  87. }
  88. private void btnLeft_Click(object sender, EventArgs e)
  89. {
  90. int i = txtValue.SelectionStart;
  91. txtValue.Focus();
  92. if (i > 0)
  93. txtValue.Select(--i, 0);
  94. }
  95. private void btnRight_Click(object sender, EventArgs e)
  96. {
  97. int i = txtValue.SelectionStart;
  98. txtValue.Focus();
  99. if (i < txtValue.Text.Trim().Length)
  100. txtValue.Select(++i, 0);
  101. }
  102. private void btnBack_Click(object sender, EventArgs e)
  103. {
  104. int i = txtValue.SelectionStart;
  105. txtValue.Focus();
  106. if (i > 0)
  107. {
  108. txtValue.Text = txtValue.Text.Trim().Substring(0, i - 1) + txtValue.Text.Trim().Substring(i);
  109. txtValue.Select(--i, 0);
  110. }
  111. }
  112. private void btnSubmit_Click(object sender, EventArgs e)
  113. {
  114. strValue = txtValue.Text.Trim();
  115. txtValue.Text = "";
  116. evoice(this, strValue);
  117. //此处还需调用加载该用户控件的界面关闭方法
  118. }
  119. }
  120. }