NumberKeybordForm.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using Sunny.UI;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Design;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using Permission;
  12. using Model;
  13. public enum NumberType
  14. {
  15. Integer=1,
  16. Double=2
  17. }
  18. public partial class NumberKeybordForm : Form
  19. {
  20. TextBox txtInput;
  21. NumberType EditType = NumberType.Double;
  22. public bool ValueChanged { get; set; }
  23. public object Value { get; set; }
  24. public bool LimitChanged { get; set; }
  25. public double Maximum { get; set; }=int.MaxValue;
  26. public double Minimum { get; set; } = int.MinValue;
  27. //public PermissionLevel InputControlLevel { get; set; } = PermissionLevel.无权限;
  28. public NumberKeybordForm()
  29. {
  30. InitializeComponent();
  31. lblPreviewValue.Text = "";
  32. txtNumber.GotFocus += TxtNumber_GotFocus;
  33. txtMinimum.GotFocus += TxtMinimum_GotFocus;
  34. txtMaximum.GotFocus += TxtMaximum_GotFocus;
  35. }
  36. public NumberKeybordForm(object value) : this()
  37. {
  38. Value = value;
  39. lblPreviewValue.Text = value.ToString();
  40. }
  41. public NumberKeybordForm(double maxValue, double minValue) : this()
  42. {
  43. txtMinimum.Text = minValue.ToString();
  44. txtMaximum.Text = maxValue.ToString();
  45. EditType = NumberType.Double;
  46. Value = 0;
  47. Maximum = maxValue;
  48. Minimum = minValue;
  49. }
  50. public NumberKeybordForm(double maxValue, double minValue, object currentValue) : this(maxValue, minValue)
  51. {
  52. Value = currentValue;
  53. lblPreviewValue.Text = currentValue.ToString();
  54. }
  55. public NumberKeybordForm(int maxValue, int minValue) : this()
  56. {
  57. txtMinimum.Text = minValue.ToString();
  58. txtMaximum.Text = maxValue.ToString();
  59. EditType = NumberType.Integer;
  60. Value = 0;
  61. Maximum = maxValue;
  62. Minimum = minValue;
  63. }
  64. public NumberKeybordForm(int maxValue, int minValue, object currentValue) : this(maxValue, minValue)
  65. {
  66. Value = currentValue;
  67. lblPreviewValue.Text = currentValue.ToString();
  68. }
  69. private void NumberKeybordForm_Load(object sender, EventArgs e)
  70. {
  71. txtInput = txtNumber;
  72. txtMinimum.TextChanged += txtMinimum_TextChanged;
  73. txtMaximum.TextChanged += txtMaximum_TextChanged;
  74. EnableEditLimitValue = PermissionManager.CurrentUser.PermissionLevel >= PermissionLevel.管理员;
  75. //PermissionHelper.SetPermissionLevel(txtMaximum, 3);
  76. //PermissionHelper.SetPermissionLevel(txtMinimum, 3);
  77. }
  78. private void txtMaximum_Click(object sender, EventArgs e)
  79. {
  80. txtMaximum.SelectAll();
  81. }
  82. private void txtMinimum_Click(object sender, EventArgs e)
  83. {
  84. txtMinimum.SelectAll();
  85. }
  86. private void txtNumber_Click(object sender, EventArgs e)
  87. {
  88. txtNumber.SelectAll();
  89. }
  90. private void TxtMaximum_GotFocus(object sender, EventArgs e)
  91. {
  92. txtInput = txtMaximum;
  93. }
  94. private void TxtMinimum_GotFocus(object sender, EventArgs e)
  95. {
  96. txtInput = txtMinimum;
  97. }
  98. private void TxtNumber_GotFocus(object sender, EventArgs e)
  99. {
  100. txtInput = txtNumber;
  101. }
  102. private bool enableEditLimitValue=false;
  103. public bool EnableEditLimitValue { get => enableEditLimitValue; set { enableEditLimitValue = value; txtMinimum.Enabled = value; txtMaximum.Enabled = value; } }
  104. public bool IsPassword
  105. {
  106. set
  107. {
  108. if (value)
  109. txtNumber.PasswordChar = '*';
  110. }
  111. }
  112. private void btnExit_Click(object sender, EventArgs e)
  113. {
  114. this.DialogResult = DialogResult.Cancel;
  115. this.Close();
  116. }
  117. private void btnNum0_Click(object sender, EventArgs e)
  118. {
  119. txtInput.Focus();
  120. SimulateInput.SimulateKey(SimulateInput.VK_NUM0);
  121. }
  122. private void btnNum1_Click(object sender, EventArgs e)
  123. {
  124. txtInput.Focus();
  125. SimulateInput.SimulateKey(SimulateInput.VK_NUM1);
  126. }
  127. private void btnNum2_Click(object sender, EventArgs e)
  128. {
  129. txtInput.Focus();
  130. SimulateInput.SimulateKey(SimulateInput.VK_NUM2);
  131. }
  132. private void btnNum3_Click(object sender, EventArgs e)
  133. {
  134. txtInput.Focus();
  135. SimulateInput.SimulateKey(SimulateInput.VK_NUM3);
  136. }
  137. private void btnNum4_Click(object sender, EventArgs e)
  138. {
  139. txtInput.Focus();
  140. SimulateInput.SimulateKey(SimulateInput.VK_NUM4);
  141. }
  142. private void btnNum5_Click(object sender, EventArgs e)
  143. {
  144. txtInput.Focus();
  145. SimulateInput.SimulateKey(SimulateInput.VK_NUM5);
  146. }
  147. private void btnNum6_Click(object sender, EventArgs e)
  148. {
  149. txtInput.Focus();
  150. SimulateInput.SimulateKey(SimulateInput.VK_NUM6);
  151. }
  152. private void btnNum7_Click(object sender, EventArgs e)
  153. {
  154. txtInput.Focus();
  155. SimulateInput.SimulateKey(SimulateInput.VK_NUM7);
  156. }
  157. private void btnNum8_Click(object sender, EventArgs e)
  158. {
  159. txtInput.Focus();
  160. SimulateInput.SimulateKey(SimulateInput.VK_NUM8);
  161. }
  162. private void btnNum9_Click(object sender, EventArgs e)
  163. {
  164. txtInput.Focus();
  165. SimulateInput.SimulateKey(SimulateInput.VK_NUM9);
  166. }
  167. private void btnPoint_Click(object sender, EventArgs e)
  168. {
  169. txtInput.Focus();
  170. SimulateInput.SimulateKey(SimulateInput.VK_DOT);
  171. }
  172. private void btnClear_Click(object sender, EventArgs e)
  173. {
  174. txtInput.Focus();
  175. txtInput.Text = "0";
  176. txtInput.SelectionStart = 1;
  177. }
  178. private void btnDel_Click(object sender, EventArgs e)
  179. {
  180. txtInput.Focus();
  181. SimulateInput.SimulateKey(SimulateInput.VK_BACKSPACE);
  182. }
  183. private void btnNegative_Click(object sender, EventArgs e)
  184. {
  185. txtInput.Focus();
  186. if (txtInput.Text.Contains("-"))
  187. {
  188. txtInput.Text=txtInput.Text.Remove(0, 1);
  189. }
  190. else
  191. {
  192. txtInput.Text= txtInput.Text.Insert(0, "-");
  193. }
  194. txtInput.SelectionStart = txtInput.Text.Length;
  195. }
  196. //string previewMaxString = "0";
  197. private void txtMaximum_TextChanged(object sender, EventArgs e)
  198. {
  199. //if (!double.TryParse(txtMaximum.Text, out double currentValue))
  200. //{
  201. // txtMaximum.Text = previewMaxString;
  202. // txtMaximum.SelectionStart = txtMaximum.Text.Length;
  203. //}
  204. //else
  205. //{
  206. // previewMaxString = txtMaximum.Text;
  207. //}
  208. }
  209. //string previewMinString = "0";
  210. private void txtMinimum_TextChanged(object sender, EventArgs e)
  211. {
  212. //if (!double.TryParse(txtMinimum.Text, out double currentValue))
  213. //{
  214. // txtMinimum.Text = previewMinString;
  215. // txtMinimum.SelectionStart= txtMinimum.Text.Length;
  216. //}
  217. //else
  218. //{
  219. // previewMinString = txtMinimum.Text;
  220. //}
  221. }
  222. //string previewNumberString = "0";
  223. private void txtNumber_TextChanged(object sender, EventArgs e)
  224. {
  225. //if (!double.TryParse(txtNumber.Text, out double currentValue))
  226. //{
  227. // txtNumber.Text = previewNumberString;
  228. // txtNumber.SelectionStart = txtNumber.Text.Length;
  229. //}
  230. //else
  231. //{
  232. // previewNumberString = txtNumber.Text;
  233. //}
  234. }
  235. private void btnEnter_Click(object sender, EventArgs e)
  236. {
  237. double maximum = 0;
  238. double minimum = 0;
  239. double input = 0;
  240. if ((txtNumber.Text.Length>0 && !double.TryParse(txtNumber.Text, out input)) || !double.TryParse(txtMaximum.Text, out maximum) || !double.TryParse(txtMinimum.Text, out minimum))
  241. {
  242. MessageBox.Show("请输入正确的数值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  243. return;
  244. }
  245. if (EnableEditLimitValue && maximum < minimum)
  246. {
  247. MessageBox.Show("数值上限不能小于下限", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  248. return;
  249. }
  250. if (txtNumber.Text.Length > 0 && (input > maximum || input < minimum))
  251. {
  252. MessageBox.Show("输入值超限制", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  253. return;
  254. }
  255. if (Maximum != maximum || Minimum != minimum)
  256. {
  257. LimitChanged = true;
  258. Maximum = maximum;
  259. Minimum = minimum;
  260. }
  261. if (txtNumber.Text.Length > 0 && Convert.ToDouble(Value.ToString()) != input)
  262. {
  263. ValueChanged = true;
  264. switch (EditType)
  265. {
  266. case NumberType.Integer:
  267. Value = (int)input;
  268. break;
  269. case NumberType.Double:
  270. Value = (double)input;
  271. break;
  272. }
  273. }
  274. DialogResult = DialogResult.OK;
  275. Close();
  276. }
  277. }