ucNumberKey2.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 partial class ucNumberKey2 : UserControl
  13. {
  14. public ucNumberKey2()
  15. {
  16. InitializeComponent();
  17. }
  18. /// <summary>
  19. /// 默认控制进行数字验证
  20. /// </summary>
  21. public bool bControl = true;
  22. public string strValue = "";
  23. /// <summary>
  24. /// 主界面调用填充数值
  25. /// </summary>
  26. public event EventSetNumKey evoice;
  27. private void btnValue_Click(object sender, EventArgs e)
  28. {
  29. Button btn = (Button)sender;
  30. int m = 0;
  31. if (txtValue1.Text.Length < 3)
  32. {
  33. iFocus = 1;
  34. setTxtValue1(btn, m);
  35. }
  36. else if (txtValue2.Text.Length < 3)
  37. {
  38. iFocus = 2;
  39. setTxtValue2(btn, m);
  40. }
  41. else if (txtValue3.Text.Length < 3)
  42. {
  43. iFocus = 3;
  44. setTxtValue3(btn, m);
  45. }
  46. }
  47. private void setTxtValue1(Button btn, int m)
  48. {
  49. int i = txtValue1.SelectionStart;
  50. txtValue1.Focus();
  51. txtValue1.Text = txtValue1.Text.Trim() + btn.Text;
  52. txtValue1.Select(++i + m, 0);
  53. }
  54. private void setTxtValue2(Button btn, int m)
  55. {
  56. int i = txtValue2.SelectionStart;
  57. txtValue2.Focus();
  58. txtValue2.Text = txtValue2.Text.Trim() + btn.Text;
  59. txtValue2.Select(++i + m, 0);
  60. }
  61. private void setTxtValue3(Button btn, int m)
  62. {
  63. int i = txtValue1.SelectionStart;
  64. txtValue3.Focus();
  65. txtValue3.Text = txtValue3.Text.Trim() + btn.Text;
  66. txtValue3.Select(++i + m, 0);
  67. }
  68. private void btnClean_Click(object sender, EventArgs e)
  69. {
  70. txtValue1.Text = "";
  71. txtValue2.Text = "";
  72. txtValue3.Text = "";
  73. }
  74. int iFocus = 1;
  75. private void btnLeft_Click(object sender, EventArgs e)
  76. {
  77. switch (iFocus)
  78. {
  79. case 1:
  80. txtValue1.Focus();
  81. break;
  82. case 2:
  83. txtValue2.Focus();
  84. break;
  85. case 3:
  86. txtValue3.Focus();
  87. break;
  88. }
  89. if (txtValue3.Focused && txtValue3.Text != "")
  90. {
  91. int i = txtValue3.SelectionStart;
  92. if (i > 0)
  93. {
  94. iFocus = 3;
  95. txtValue3.Focus();
  96. txtValue3.Select(--i, 0);
  97. }
  98. else
  99. {
  100. iFocus = 2;
  101. txtValue2.Focus();
  102. txtValue2.Select(3, 0);
  103. }
  104. }
  105. else if (txtValue2.Focused && txtValue2.Text != "")
  106. {
  107. int i = txtValue2.SelectionStart;
  108. if (i > 0)
  109. {
  110. iFocus = 2;
  111. txtValue2.Focus();
  112. txtValue2.Select(--i, 0);
  113. }
  114. else
  115. {
  116. iFocus = 1;
  117. txtValue1.Focus();
  118. txtValue1.Select(3, 0);
  119. }
  120. }
  121. else if (txtValue1.Focused && txtValue1.Text != "")
  122. {
  123. iFocus = 1;
  124. int i = txtValue1.SelectionStart;
  125. txtValue1.Focus();
  126. if (i > 0)
  127. txtValue1.Select(--i, 0);
  128. }
  129. }
  130. private void btnRight_Click(object sender, EventArgs e)
  131. {
  132. switch (iFocus)
  133. {
  134. case 1:
  135. txtValue1.Focus();
  136. break;
  137. case 2:
  138. txtValue2.Focus();
  139. break;
  140. case 3:
  141. txtValue3.Focus();
  142. break;
  143. }
  144. if (txtValue1.Focused && txtValue1.Text != "")
  145. {
  146. int i = txtValue1.SelectionStart;
  147. txtValue1.Focus();
  148. if (i < txtValue1.Text.Trim().Length)
  149. {
  150. txtValue1.Select(++i, 0);
  151. }
  152. else
  153. {
  154. iFocus = 2;
  155. txtValue2.Focus();
  156. }
  157. }
  158. else if (txtValue2.Focused && txtValue2.Text != "")
  159. {
  160. int i = txtValue2.SelectionStart;
  161. txtValue2.Focus();
  162. if (i < txtValue2.Text.Trim().Length)
  163. {
  164. txtValue2.Select(++i, 0);
  165. }
  166. else
  167. {
  168. iFocus = 3;
  169. txtValue3.Focus();
  170. }
  171. }
  172. else if (txtValue3.Focused && txtValue3.Text != "")
  173. {
  174. int i = txtValue3.SelectionStart;
  175. iFocus = 3;
  176. txtValue3.Focus();
  177. if (i < txtValue3.Text.Trim().Length)
  178. {
  179. txtValue3.Select(++i, 0);
  180. }
  181. }
  182. }
  183. private void btnBack_Click(object sender, EventArgs e)
  184. {
  185. if (txtValue3.Text != "")
  186. {
  187. iFocus = 3;
  188. int i = txtValue3.SelectionStart;
  189. txtValue3.Focus();
  190. if (i > 0)
  191. {
  192. txtValue3.Text = txtValue3.Text.Trim().Substring(0, i - 1) + txtValue3.Text.Trim().Substring(i);
  193. txtValue3.Select(--i, 0);
  194. }
  195. }
  196. else if (txtValue2.Text != "")
  197. {
  198. iFocus = 2;
  199. int i = txtValue2.SelectionStart;
  200. txtValue2.Focus();
  201. if (i > 0)
  202. {
  203. txtValue2.Text = txtValue2.Text.Trim().Substring(0, i - 1) + txtValue2.Text.Trim().Substring(i);
  204. txtValue2.Select(--i, 0);
  205. }
  206. }
  207. else if (txtValue1.Text != "")
  208. {
  209. iFocus = 1;
  210. int i = txtValue1.SelectionStart;
  211. txtValue1.Focus();
  212. if (i > 0)
  213. {
  214. txtValue1.Text = txtValue1.Text.Trim().Substring(0, i - 1) + txtValue1.Text.Trim().Substring(i);
  215. txtValue1.Select(--i, 0);
  216. }
  217. }
  218. }
  219. private void btnSubmit_Click(object sender, EventArgs e)
  220. {
  221. strValue = txtValue1.Text.Trim() + txtValue2.Text.Trim() + txtValue3.Text.Trim();
  222. txtValue1.Text = "";
  223. txtValue2.Text = "";
  224. txtValue3.Text = "";
  225. evoice(this, strValue);
  226. //此处还需调用加载该用户控件的界面关闭方法
  227. }
  228. }
  229. }