FrmNumKeybord.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WireProcessMachine
  10. {
  11. public partial class FrmKeybord : Form
  12. {
  13. static bool NumKeyboardEnable=true;
  14. private bool falg = false;
  15. private double MaxValue;
  16. private double MinValue;
  17. //public bool TypePassWord;
  18. private string str = "";
  19. #region
  20. public FrmKeybord()
  21. {
  22. InitializeComponent();
  23. this.ControlBox = false;
  24. this.MaximizeBox = false;
  25. foreach (Control item in panel1.Controls)
  26. {
  27. if (item is Button)
  28. {
  29. Button btn = (Button)item;
  30. btn.Click += btnNumber_Click;
  31. }
  32. }
  33. btnSub.Click -= btnNumber_Click;
  34. }
  35. public new DialogResult ShowDialog()
  36. {
  37. if (NumKeyboardEnable == false)
  38. {
  39. this.Tag = str;
  40. return DialogResult.Abort;
  41. }
  42. else
  43. return base.ShowDialog();
  44. }
  45. public bool TypePassword
  46. {
  47. set
  48. {
  49. if(value)
  50. txtNumber.PasswordChar = '*';
  51. }
  52. }
  53. public FrmKeybord(double maxValue, double minValue) : this()
  54. {
  55. this.MaxValue = maxValue;
  56. this.MinValue = minValue;
  57. lblMax.Text = maxValue.ToString();
  58. lblMin.Text = minValue.ToString();
  59. this.txtNumber.SelectAll();
  60. }
  61. public FrmKeybord(double maxValue, double minValue, string text) : this()
  62. {
  63. this.MaxValue = maxValue;
  64. this.MinValue = minValue;
  65. if (text == null || text == "")
  66. {
  67. this.txtNumber.Text = "0";
  68. str = "";
  69. }
  70. else
  71. {
  72. this.txtNumber.Text = text;
  73. str = text;
  74. }
  75. lblMax.Text = maxValue.ToString();
  76. lblMin.Text = minValue.ToString();
  77. this.txtNumber.SelectAll();
  78. }
  79. private void btnExit_Click(object sender, EventArgs e)
  80. {
  81. this.Tag = str;
  82. this.DialogResult = DialogResult.Cancel;
  83. this.Close();
  84. }
  85. private void FrmKeybord_FormClosing(object sender, FormClosingEventArgs e)
  86. {
  87. }
  88. private void btnNumber_Click(object sender, EventArgs e)
  89. {
  90. if (falg == false)
  91. {
  92. this.txtNumber.Text = "";
  93. falg = true;
  94. }
  95. if (((Button)sender).Text != ".")
  96. {
  97. if (txtNumber.Text.Substring(0) == "0")
  98. {
  99. txtNumber.Text = txtNumber.Text.Substring(1);
  100. }
  101. this.txtNumber.Text += ((Button)sender).Text;
  102. }
  103. else
  104. {
  105. if (((Button)sender).Text == "." && !this.txtNumber.Text.Contains("."))
  106. {
  107. if (txtNumber.Text.Substring(0) == "0")
  108. {
  109. this.txtNumber.Text = "0" + ((Button)sender).Text;
  110. //txtNumber.Text = txtNumber.Text.Substring(1);
  111. }
  112. else if (txtNumber.Text.Substring(0) == "")
  113. {
  114. this.txtNumber.Text = "0" + ((Button)sender).Text;
  115. }
  116. else
  117. {
  118. this.txtNumber.Text += ((Button)sender).Text;
  119. }
  120. }
  121. }
  122. }
  123. #region 输入数据
  124. private void btnEnter_Click(object sender, EventArgs e)
  125. {
  126. double number = 0;
  127. double.TryParse(this.txtNumber.Text,out number);
  128. double x =this.txtNumber.Text==""?0: number;
  129. if (x > this.MaxValue || x < this.MinValue)
  130. {
  131. DialogResult result = MessageBox.Show("输入数值范围错误!重新输入!" + "\r\n" + "Over the value limit!",
  132. "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  133. if (result == DialogResult.OK)
  134. {
  135. this.txtNumber.Focus();
  136. this.txtNumber.SelectAll();
  137. falg = false;
  138. return;
  139. }
  140. }
  141. else
  142. {
  143. ReturnValue();
  144. this.DialogResult = DialogResult.OK;
  145. this.Close();
  146. }
  147. }
  148. #endregion
  149. private void ReturnValue()
  150. {
  151. if (this.txtNumber.Text.Length > 1 && !this.txtNumber.Text.Contains("-"))
  152. {
  153. this.Tag = this.txtNumber.Text;
  154. }
  155. else
  156. {
  157. if (this.txtNumber.Text.Length > 1 && this.txtNumber.Text.Contains("-"))
  158. {
  159. this.Tag = "-" + this.txtNumber.Text.Substring(1, this.txtNumber.Text.Length - 1);
  160. }
  161. else if (this.txtNumber.Text.Length == 1)
  162. {
  163. this.Tag = this.txtNumber.Text;
  164. }
  165. }
  166. }
  167. #region 删除一个数字
  168. private void btnDel_Click(object sender, EventArgs e)
  169. {
  170. if (txtNumber.Text.Length > 1)
  171. {
  172. txtNumber.Text = txtNumber.Text.Substring(0, txtNumber.Text.Length - 1);
  173. }
  174. else
  175. {
  176. txtNumber.Text = "0";
  177. }
  178. }
  179. #endregion
  180. #region 清除数字
  181. private void btnClear_Click(object sender, EventArgs e)
  182. {
  183. this.txtNumber.Text = "";
  184. }
  185. #endregion
  186. #region 添加正负号
  187. private void btnSub_Click(object sender, EventArgs e)
  188. {
  189. if (falg == false)
  190. {
  191. this.txtNumber.Text = "";
  192. falg = true;
  193. }
  194. if (!txtNumber.Text.Contains("-"))
  195. {
  196. if (txtNumber.Text.Substring(0) == "0")
  197. {
  198. txtNumber.Text = txtNumber.Text.Substring(1);
  199. }
  200. this.txtNumber.Text = "-" + this.txtNumber.Text;
  201. }
  202. else
  203. {
  204. if (txtNumber.Text.Length > 1)
  205. {
  206. txtNumber.Text = txtNumber.Text.Substring(1, txtNumber.Text.Length - 1);
  207. }
  208. }
  209. }
  210. #endregion
  211. private void FrmKeybord_FormClosed(object sender, FormClosedEventArgs e)
  212. {
  213. }
  214. }
  215. #endregion
  216. }