UCNumTextBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCNumTextBox.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCNumTextBox.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [DefaultEvent("NumChanged")]
  32. public partial class UCNumTextBox : UserControl
  33. {
  34. /// <summary>
  35. /// Occurs when [show key border event].
  36. /// </summary>
  37. [Description("弹出输入键盘时发生"), Category("自定义")]
  38. public event EventHandler ShowKeyBorderEvent;
  39. /// <summary>
  40. /// Occurs when [hide key border event].
  41. /// </summary>
  42. [Description("关闭输入键盘时发生"), Category("自定义")]
  43. public event EventHandler HideKeyBorderEvent;
  44. /// <summary>
  45. /// Occurs when [number changed].
  46. /// </summary>
  47. [Description("数字改变时发生"), Category("自定义")]
  48. public event EventHandler NumChanged;
  49. /// <summary>
  50. /// 输入类型
  51. /// </summary>
  52. /// <value>The type of the input.</value>
  53. [Description("输入类型"), Category("自定义")]
  54. public TextInputType InputType
  55. {
  56. get
  57. {
  58. return txtNum.InputType;
  59. }
  60. set
  61. {
  62. if (value == TextInputType.NotControl)
  63. {
  64. return;
  65. }
  66. txtNum.InputType = value;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets or sets a value indicating whether this instance is number can input.
  71. /// </summary>
  72. /// <value><c>true</c> if this instance is number can input; otherwise, <c>false</c>.</value>
  73. [Description("数字是否可手动编辑"), Category("自定义")]
  74. public bool IsNumCanInput
  75. {
  76. get
  77. {
  78. return txtNum.Enabled;
  79. }
  80. set
  81. {
  82. txtNum.Enabled = value;
  83. }
  84. }
  85. /// <summary>
  86. /// 当InputType为数字类型时,能输入的最大值
  87. /// </summary>
  88. /// <value>The maximum value.</value>
  89. [Description("当InputType为数字类型时,能输入的最大值。")]
  90. public decimal MaxValue
  91. {
  92. get
  93. {
  94. return this.txtNum.MaxValue;
  95. }
  96. set
  97. {
  98. this.txtNum.MaxValue = value;
  99. }
  100. }
  101. /// <summary>
  102. /// 当InputType为数字类型时,能输入的最小值
  103. /// </summary>
  104. /// <value>The minimum value.</value>
  105. [Description("当InputType为数字类型时,能输入的最小值。")]
  106. public decimal MinValue
  107. {
  108. get
  109. {
  110. return this.txtNum.MinValue;
  111. }
  112. set
  113. {
  114. this.txtNum.MinValue = value;
  115. }
  116. }
  117. /// <summary>
  118. /// The key board type
  119. /// </summary>
  120. private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderNum;
  121. /// <summary>
  122. /// Gets or sets the type of the key board.
  123. /// </summary>
  124. /// <value>The type of the key board.</value>
  125. [Description("键盘样式"), Category("自定义")]
  126. public KeyBoardType KeyBoardType
  127. {
  128. get { return keyBoardType; }
  129. set { keyBoardType = value; }
  130. }
  131. /// <summary>
  132. /// Gets or sets the number.
  133. /// </summary>
  134. /// <value>The number.</value>
  135. [Description("数值"), Category("自定义")]
  136. public decimal Num
  137. {
  138. get { return txtNum.Text.ToDecimal(); }
  139. set { txtNum.Text = value.ToString(); }
  140. }
  141. /// <summary>
  142. /// 获取或设置控件显示的文字的字体。
  143. /// </summary>
  144. /// <value>The font.</value>
  145. /// <PermissionSet>
  146. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  147. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  148. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  149. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  150. /// </PermissionSet>
  151. [Description("字体"), Category("自定义")]
  152. public new Font Font
  153. {
  154. get
  155. {
  156. return txtNum.Font;
  157. }
  158. set
  159. {
  160. txtNum.Font = value;
  161. }
  162. }
  163. /// <summary>
  164. /// Occurs when [add click].
  165. /// </summary>
  166. [Description("增加按钮点击事件"), Category("自定义")]
  167. public event EventHandler AddClick;
  168. /// <summary>
  169. /// Occurs when [minus click].
  170. /// </summary>
  171. [Description("减少按钮点击事件"), Category("自定义")]
  172. public event EventHandler MinusClick;
  173. private decimal increment = 1;
  174. [Description("递增量,大于0的数值"), Category("自定义")]
  175. public decimal Increment
  176. {
  177. get { return increment; }
  178. set
  179. {
  180. if (value <= 0)
  181. return;
  182. increment = value;
  183. }
  184. }
  185. /// <summary>
  186. /// Initializes a new instance of the <see cref="UCNumTextBox" /> class.
  187. /// </summary>
  188. public UCNumTextBox()
  189. {
  190. InitializeComponent();
  191. txtNum.TextChanged += txtNum_TextChanged;
  192. }
  193. /// <summary>
  194. /// Handles the TextChanged event of the txtNum control.
  195. /// </summary>
  196. /// <param name="sender">The source of the event.</param>
  197. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  198. void txtNum_TextChanged(object sender, EventArgs e)
  199. {
  200. if (NumChanged != null)
  201. {
  202. NumChanged(txtNum.Text.ToString(), e);
  203. }
  204. }
  205. /// <summary>
  206. /// The m FRM anchor
  207. /// </summary>
  208. Forms.FrmAnchor m_frmAnchor;
  209. /// <summary>
  210. /// Handles the MouseDown event of the txtNum control.
  211. /// </summary>
  212. /// <param name="sender">The source of the event.</param>
  213. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  214. private void txtNum_MouseDown(object sender, MouseEventArgs e)
  215. {
  216. if (IsNumCanInput)
  217. {
  218. if (KeyBoardType != HZH_Controls.Controls.KeyBoardType.Null)
  219. {
  220. switch (keyBoardType)
  221. {
  222. case KeyBoardType.UCKeyBorderAll_EN:
  223. UCKeyBorderAll keyAll = new UCKeyBorderAll();
  224. keyAll.RetractClike += (a, b) => { m_frmAnchor.Hide(); };
  225. keyAll.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
  226. m_frmAnchor = new Forms.FrmAnchor(this, keyAll);
  227. m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged;
  228. m_frmAnchor.Show(this.FindForm());
  229. break;
  230. case KeyBoardType.UCKeyBorderNum:
  231. UCKeyBorderNum keyNum = new UCKeyBorderNum();
  232. keyNum.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
  233. m_frmAnchor = new Forms.FrmAnchor(this, keyNum);
  234. m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged;
  235. m_frmAnchor.Show(this.FindForm());
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. /// <summary>
  242. /// Handles the VisibleChanged event of the m_frmAnchor control.
  243. /// </summary>
  244. /// <param name="sender">The source of the event.</param>
  245. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  246. void m_frmAnchor_VisibleChanged(object sender, EventArgs e)
  247. {
  248. if (m_frmAnchor.Visible)
  249. {
  250. if (ShowKeyBorderEvent != null)
  251. {
  252. ShowKeyBorderEvent(this, null);
  253. }
  254. }
  255. else
  256. {
  257. if (HideKeyBorderEvent != null)
  258. {
  259. HideKeyBorderEvent(this, null);
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// Numbers the add click.
  265. /// </summary>
  266. public void NumAddClick()
  267. {
  268. btnAdd_MouseDown(null, null);
  269. }
  270. /// <summary>
  271. /// Numbers the minus click.
  272. /// </summary>
  273. public void NumMinusClick()
  274. {
  275. btnMinus_MouseDown(null, null);
  276. }
  277. /// <summary>
  278. /// Handles the MouseDown event of the btnAdd control.
  279. /// </summary>
  280. /// <param name="sender">The source of the event.</param>
  281. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  282. private void btnAdd_MouseDown(object sender, MouseEventArgs e)
  283. {
  284. if (AddClick != null)
  285. {
  286. AddClick(this, e);
  287. }
  288. decimal dec = this.txtNum.Text.ToDecimal();
  289. dec += increment;
  290. txtNum.Text = dec.ToString();
  291. }
  292. /// <summary>
  293. /// Handles the MouseDown event of the btnMinus control.
  294. /// </summary>
  295. /// <param name="sender">The source of the event.</param>
  296. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  297. private void btnMinus_MouseDown(object sender, MouseEventArgs e)
  298. {
  299. if (MinusClick != null)
  300. {
  301. MinusClick(this, e);
  302. }
  303. decimal dec = this.txtNum.Text.ToDecimal();
  304. dec -= increment; ;
  305. txtNum.Text = dec.ToString();
  306. }
  307. /// <summary>
  308. /// Handles the Load event of the UCNumTextBox control.
  309. /// </summary>
  310. /// <param name="sender">The source of the event.</param>
  311. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  312. private void UCNumTextBox_Load(object sender, EventArgs e)
  313. {
  314. this.txtNum.BackColor = this.BackColor;
  315. }
  316. /// <summary>
  317. /// Handles the FontChanged event of the txtNum control.
  318. /// </summary>
  319. /// <param name="sender">The source of the event.</param>
  320. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  321. private void txtNum_FontChanged(object sender, EventArgs e)
  322. {
  323. txtNum.Location = new Point(txtNum.Location.X, (this.Height - txtNum.Height) / 2);
  324. }
  325. /// <summary>
  326. /// Handles the BackColorChanged event of the UCNumTextBox control.
  327. /// </summary>
  328. /// <param name="sender">The source of the event.</param>
  329. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  330. private void UCNumTextBox_BackColorChanged(object sender, EventArgs e)
  331. {
  332. Color c = this.BackColor;
  333. Control control = this;
  334. while (c == Color.Transparent)
  335. {
  336. control = control.Parent;
  337. if (control == null)
  338. break;
  339. c = control.BackColor;
  340. }
  341. if (c == Color.Transparent)
  342. return;
  343. txtNum.BackColor = c;
  344. }
  345. }
  346. }