TextBoxEx2.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Caliburn.Micro;
  2. using MECF.Framework.UI.Client.CenterViews.Dialogs;
  3. using MECF.Framework.UI.Client.ClientBase;
  4. using OpenSEMI.ClientBase;
  5. using OpenSEMI.Ctrlib.Controls;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Input;
  15. using TextBox2 = OpenSEMI.Ctrlib.Controls.TextBoxEx;
  16. namespace MECF.Framework.UI.Client.Themes.lightgreen
  17. {
  18. public partial class TextBoxEx2 : ResourceDictionary
  19. {
  20. private static Regex RegNumber = new Regex("^[0-9]+$");
  21. private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
  22. private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
  23. private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
  24. private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
  25. /// <summary>
  26. /// 是否数字字符串
  27. /// </summary>
  28. /// <param name="inputData">输入字符串</param>
  29. /// <returns></returns>
  30. public static bool IsNumber(string inputData)
  31. {
  32. Match m = RegNumber.Match(inputData);
  33. return m.Success;
  34. }
  35. /// <summary>
  36. /// 是否数字字符串 可带正负号
  37. /// </summary>
  38. /// <param name="inputData">输入字符串</param>
  39. /// <returns></returns>
  40. public static bool IsNumberSign(string inputData)
  41. {
  42. Match m = RegNumberSign.Match(inputData);
  43. return m.Success;
  44. }
  45. /// <summary>
  46. /// 是否是浮点数
  47. /// </summary>
  48. /// <param name="inputData">输入字符串</param>
  49. /// <returns></returns>
  50. public static bool IsDecimal(string inputData)
  51. {
  52. Match m = RegDecimal.Match(inputData);
  53. return m.Success;
  54. }
  55. /// <summary>
  56. /// 是否是浮点数 可带正负号
  57. /// </summary>
  58. /// <param name="inputData">输入字符串</param>
  59. /// <returns></returns>
  60. public static bool IsDecimalSign(string inputData)
  61. {
  62. Match m = RegDecimalSign.Match(inputData);
  63. return m.Success;
  64. }
  65. public void TextBoxExOnClick(object sender, MouseButtonEventArgs e)
  66. {
  67. if (sender is TextBox2)
  68. {
  69. TextBox2 textBox = sender as TextBox2;
  70. string strRet = Show(sender, textBox.Text);
  71. if (string.IsNullOrEmpty(strRet)) return;
  72. if (!CheckEditBoxMode(textBox, strRet))
  73. {
  74. DialogBox.ShowInfo($"Please enter the corresponding type data {textBox.EditBoxMode.ToString()}");
  75. return;
  76. }
  77. if (!textBox.AllowIsOutOfRangeSaveChange && textBox.IsOutOfRange(double.Parse(strRet)))
  78. {
  79. DialogBox.ShowInfo($"Out Of Range{textBox.MinValue}-{textBox.MaxValue}");
  80. return;
  81. }
  82. if (textBox.EditBoxMode == EditBoxMode.SignInteger || textBox.EditBoxMode == EditBoxMode.UnSignInteger || textBox.EditBoxMode == EditBoxMode.Decimal || textBox.EditBoxMode == EditBoxMode.UnSignDecimal)
  83. {
  84. var controlValue = double.Parse(strRet);
  85. if (textBox.KeepDecimals != -1)
  86. {
  87. if (textBox.KeepIntegers != -1)
  88. {
  89. StringBuilder strFormat = new StringBuilder();
  90. for (int i = 0; i < textBox.KeepIntegers; i++)
  91. {
  92. strFormat.Append("0");
  93. }
  94. strFormat.Append(".");
  95. for (int i = 0; i < textBox.KeepDecimals; i++)
  96. {
  97. strFormat.Append("0");
  98. }
  99. textBox.Text = controlValue.ToString(strFormat.ToString());
  100. }
  101. else
  102. { textBox.Text = controlValue.ToString($"f{textBox.KeepDecimals}"); }
  103. }
  104. else
  105. {
  106. if (textBox.KeepIntegers != -1)
  107. {
  108. textBox.Text = ((int)controlValue).ToString($"D{textBox.KeepIntegers}");
  109. }
  110. else
  111. { textBox.Text = controlValue.ToString(); }
  112. }
  113. }
  114. else
  115. {
  116. textBox.Text = strRet;
  117. }
  118. }
  119. }
  120. private bool CheckEditBoxMode(TextBox2 boxEx, string value)
  121. {
  122. bool modeIsOk = false;
  123. switch (boxEx.EditBoxMode)
  124. {
  125. case OpenSEMI.Ctrlib.Controls.EditBoxMode.Default:
  126. case OpenSEMI.Ctrlib.Controls.EditBoxMode.Email:
  127. case OpenSEMI.Ctrlib.Controls.EditBoxMode.Time:
  128. case OpenSEMI.Ctrlib.Controls.EditBoxMode.FileName:
  129. modeIsOk = true;
  130. break;
  131. case OpenSEMI.Ctrlib.Controls.EditBoxMode.SignInteger:
  132. modeIsOk = IsNumberSign(value);
  133. break;
  134. case OpenSEMI.Ctrlib.Controls.EditBoxMode.UnSignInteger:
  135. modeIsOk = IsNumber(value);
  136. break;
  137. case OpenSEMI.Ctrlib.Controls.EditBoxMode.Decimal:
  138. modeIsOk = IsDecimalSign(value) || IsNumberSign(value);
  139. break;
  140. case OpenSEMI.Ctrlib.Controls.EditBoxMode.UnSignDecimal:
  141. modeIsOk = IsDecimal(value) || IsNumber(value);
  142. break;
  143. default:
  144. break;
  145. }
  146. return modeIsOk;
  147. }
  148. private string Show(object sender, string strDefaultValue)
  149. {
  150. Control control = sender as Control;
  151. string strRet = string.Empty;
  152. if (control.Tag != null && control.Tag.ToString().Equals("Number"))
  153. {
  154. var point = control.PointFromScreen(new Point(0, 0));
  155. double x = SystemParameters.WorkArea.Width;
  156. double y = SystemParameters.WorkArea.Height;
  157. NumberKeyboard numberKeyboard = new NumberKeyboard("", strDefaultValue);
  158. if (-point.Y + control.ActualHeight + 5 + numberKeyboard.Height < y)
  159. {
  160. numberKeyboard.Top = -point.Y + control.ActualHeight + 5;
  161. }
  162. else
  163. {
  164. numberKeyboard.Top = -point.Y - numberKeyboard.Height - 5;
  165. }
  166. if (-point.X + numberKeyboard.Width < x)
  167. {
  168. numberKeyboard.Left = -point.X;
  169. }
  170. else
  171. {
  172. numberKeyboard.Left = -point.X - (numberKeyboard.Width - control.ActualWidth);
  173. }
  174. if ((bool)numberKeyboard.ShowDialog()) strRet = numberKeyboard.ValueString;
  175. }
  176. else if (control.Tag != null && control.Tag.ToString().Contains("None"))
  177. {
  178. return string.Empty;
  179. }
  180. else
  181. {
  182. FullKeyboard fullKeyboard = new FullKeyboard("", strDefaultValue);
  183. var point = control.PointFromScreen(new Point(0, 0));
  184. double x = SystemParameters.WorkArea.Width;
  185. double y = SystemParameters.WorkArea.Height;
  186. if (-point.Y + control.ActualHeight + 5 + fullKeyboard.Height < y)
  187. {
  188. fullKeyboard.Top = -point.Y + control.ActualHeight + 5;
  189. }
  190. else
  191. {
  192. fullKeyboard.Top = -point.Y - fullKeyboard.Height - 5;
  193. }
  194. if (-point.X + fullKeyboard.Width < x)
  195. {
  196. fullKeyboard.Left = -point.X;
  197. }
  198. else
  199. {
  200. fullKeyboard.Left = -point.X - (fullKeyboard.Width - control.ActualWidth);
  201. }
  202. if ((bool)fullKeyboard.ShowDialog()) strRet = fullKeyboard.ValueString;
  203. }
  204. return strRet;
  205. }
  206. }
  207. }