NumKeybordForm.cs 5.9 KB

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