NumberKeyborardNoMinus.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using OpenSEMI.ClientBase;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace MECF.Framework.UI.Client.CenterViews.Dialogs
  18. {
  19. /// <summary>
  20. /// NumberKeyboard.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class NumberKeyborardNoMinus : Window
  23. {
  24. //public FullKeyboard()
  25. //{
  26. // InitializeComponent();
  27. //}
  28. private String valueString;
  29. private double _maxValue = double.MaxValue;
  30. private double _minValue = double.MinValue;
  31. private bool _isValidate;
  32. public String ValueString
  33. {
  34. get
  35. {
  36. ValueHandler(ref valueString);
  37. if (valueString == "")
  38. { return "0"; }
  39. else
  40. {
  41. return valueString;
  42. }
  43. }
  44. }
  45. public NumberKeyborardNoMinus(String inputTitle, String inputvalue, bool isValidate = false, double maxValue = double.MaxValue, double minValue = 0)
  46. {
  47. InitializeComponent();
  48. ValueHandler(ref inputvalue);
  49. tbValue.Text = inputvalue;
  50. valueString = inputvalue;
  51. _isValidate = isValidate;
  52. _maxValue = maxValue;
  53. _minValue = minValue;
  54. }
  55. private void ValueHandler(ref string inputvalue)
  56. {
  57. if (!string.IsNullOrEmpty(inputvalue))
  58. {
  59. if (Regex.Matches(inputvalue, ":").Count == 0 && inputvalue.Length > 1)
  60. {
  61. //if(inputvalue.Split('.').Length<=1)
  62. double outValue = 0;
  63. double.TryParse(inputvalue.ToString(), out outValue);
  64. inputvalue = outValue.ToString();
  65. }
  66. }
  67. }
  68. private bool IsFristClick = true;
  69. //通过判断按钮的content属性来做对应处理,以简化大量按钮的编程
  70. private void ButtonGrid_Click(object sender, RoutedEventArgs e)
  71. {
  72. Button clickedButton = (Button)e.OriginalSource; //获取click事件触发源,即按了的按钮
  73. if ((String)clickedButton.Content == "DEL")
  74. {
  75. if (tbValue.Text.Length > 0)
  76. {
  77. tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - 1);
  78. }
  79. }
  80. else if ((String)clickedButton.Content == "Clear")
  81. {
  82. tbValue.Text = "";
  83. }
  84. else if ((String)clickedButton.Content == "Cancel")
  85. {
  86. this.DialogResult = false;
  87. this.Close();
  88. }
  89. else if ((String)clickedButton.Content == "OK")
  90. {
  91. valueString = tbValue.Text;
  92. if (Regex.Matches(valueString, ":").Count > 0)
  93. {
  94. var value = Regex.IsMatch(valueString, "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]");
  95. if (!value)
  96. {
  97. DialogBox.ShowWarning("输入格式不正确");
  98. return;
  99. }
  100. }
  101. double dtempValue;
  102. if (double.TryParse(tbValue.Text, out dtempValue))
  103. {
  104. if (_isValidate)
  105. {
  106. if (dtempValue > _maxValue)
  107. {
  108. DialogBox.ShowWarning($"输入数值超过上限值.{_maxValue}");
  109. return;
  110. }
  111. if (dtempValue < _minValue)
  112. {
  113. DialogBox.ShowWarning($"输入数值低于下限值.{_minValue}");
  114. return;
  115. }
  116. }
  117. }
  118. this.DialogResult = true;
  119. this.Close();
  120. }
  121. else if ((String)clickedButton.Content == "-")
  122. {
  123. if (IsFristClick)
  124. {
  125. IsFristClick = false;
  126. tbValue.Text = "";
  127. }
  128. if (tbValue.Text == "")
  129. {
  130. tbValue.Text += (String)clickedButton.Content;
  131. }
  132. else
  133. {
  134. if (tbValue.Text.Substring(0, 1) == "-")
  135. {
  136. tbValue.Text = tbValue.Text.Substring(1, tbValue.Text.Length - 1);
  137. }
  138. else
  139. {
  140. tbValue.Text = $"-{tbValue.Text }";
  141. }
  142. }
  143. }
  144. else if ((String)clickedButton.Content == "A/a")
  145. {
  146. int count = ButtonGrid.Children.Count;
  147. for (int i = 10; i < count - 4; i++)
  148. {
  149. Button buttonTemp = ButtonGrid.Children[i] as Button;
  150. String contentTemp = buttonTemp.Content as String;
  151. buttonTemp.Content = contentTemp[0] > 90 ? contentTemp.ToUpper() : contentTemp.ToLower();
  152. }
  153. }
  154. else
  155. {
  156. if (IsFristClick)
  157. {
  158. IsFristClick = false;
  159. tbValue.Text = "";
  160. }
  161. tbValue.Text += (String)clickedButton.Content;
  162. }
  163. }
  164. }
  165. }