TextBoxEx.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="TextBoxEx.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.Diagnostics;
  20. using System.Drawing;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class TextBoxEx.
  28. /// Implements the <see cref="System.Windows.Forms.TextBox" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.TextBox" />
  31. public partial class TextBoxEx : TextBox
  32. {
  33. /// <summary>
  34. /// The BLN focus
  35. /// </summary>
  36. private bool blnFocus = false;
  37. /// <summary>
  38. /// The prompt text
  39. /// </summary>
  40. private string _promptText = string.Empty;
  41. /// <summary>
  42. /// The prompt font
  43. /// </summary>
  44. private Font _promptFont = new Font("微软雅黑", 15f, FontStyle.Regular, GraphicsUnit.Pixel);
  45. /// <summary>
  46. /// The prompt color
  47. /// </summary>
  48. private Color _promptColor = Color.Gray;
  49. /// <summary>
  50. /// My rectangle
  51. /// </summary>
  52. private Rectangle _myRectangle = Rectangle.FromLTRB(1, 3, 1000, 3);
  53. /// <summary>
  54. /// The input type
  55. /// </summary>
  56. private TextInputType _inputType = TextInputType.NotControl;
  57. /// <summary>
  58. /// The regex pattern
  59. /// </summary>
  60. private string _regexPattern = "";
  61. /// <summary>
  62. /// The m string old value
  63. /// </summary>
  64. private string m_strOldValue = string.Empty;
  65. /// <summary>
  66. /// The maximum value
  67. /// </summary>
  68. private decimal _maxValue = 1000000m;
  69. /// <summary>
  70. /// The minimum value
  71. /// </summary>
  72. private decimal _minValue = -1000000m;
  73. /// <summary>
  74. /// The decimal length
  75. /// </summary>
  76. private int _decLength = 2;
  77. /// <summary>
  78. /// 水印文字
  79. /// </summary>
  80. /// <value>The prompt text.</value>
  81. [Description("水印文字"), Category("自定义")]
  82. public string PromptText
  83. {
  84. get
  85. {
  86. return this._promptText;
  87. }
  88. set
  89. {
  90. this._promptText = value;
  91. this.OnPaint(null);
  92. }
  93. }
  94. /// <summary>
  95. /// Gets or sets the prompt font.
  96. /// </summary>
  97. /// <value>The prompt font.</value>
  98. [Description("水印字体"), Category("自定义")]
  99. public Font PromptFont
  100. {
  101. get
  102. {
  103. return this._promptFont;
  104. }
  105. set
  106. {
  107. this._promptFont = value;
  108. }
  109. }
  110. /// <summary>
  111. /// Gets or sets the color of the prompt.
  112. /// </summary>
  113. /// <value>The color of the prompt.</value>
  114. [Description("水印颜色"), Category("自定义")]
  115. public Color PromptColor
  116. {
  117. get
  118. {
  119. return this._promptColor;
  120. }
  121. set
  122. {
  123. this._promptColor = value;
  124. }
  125. }
  126. /// <summary>
  127. /// Gets or sets my rectangle.
  128. /// </summary>
  129. /// <value>My rectangle.</value>
  130. public Rectangle MyRectangle
  131. {
  132. get;
  133. set;
  134. }
  135. /// <summary>
  136. /// Gets or sets the old text.
  137. /// </summary>
  138. /// <value>The old text.</value>
  139. public string OldText
  140. {
  141. get;
  142. set;
  143. }
  144. /// <summary>
  145. /// Gets or sets the type of the input.
  146. /// </summary>
  147. /// <value>The type of the input.</value>
  148. [Description("获取或设置一个值,该值指示文本框中的文本输入类型。")]
  149. public TextInputType InputType
  150. {
  151. get
  152. {
  153. return this._inputType;
  154. }
  155. set
  156. {
  157. this._inputType = value;
  158. if (value != TextInputType.NotControl)
  159. {
  160. TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
  161. TextChanged += new EventHandler(this.TextBoxEx_TextChanged);
  162. }
  163. else
  164. {
  165. TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。
  171. /// </summary>
  172. /// <value>The regex pattern.</value>
  173. [Description("获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。")]
  174. public string RegexPattern
  175. {
  176. get
  177. {
  178. return this._regexPattern;
  179. }
  180. set
  181. {
  182. this._regexPattern = value;
  183. }
  184. }
  185. /// <summary>
  186. /// 当InputType为数字类型时,能输入的最大值
  187. /// </summary>
  188. /// <value>The maximum value.</value>
  189. [Description("当InputType为数字类型时,能输入的最大值。")]
  190. public decimal MaxValue
  191. {
  192. get
  193. {
  194. return this._maxValue;
  195. }
  196. set
  197. {
  198. this._maxValue = value;
  199. }
  200. }
  201. /// <summary>
  202. /// 当InputType为数字类型时,能输入的最小值
  203. /// </summary>
  204. /// <value>The minimum value.</value>
  205. [Description("当InputType为数字类型时,能输入的最小值。")]
  206. public decimal MinValue
  207. {
  208. get
  209. {
  210. return this._minValue;
  211. }
  212. set
  213. {
  214. this._minValue = value;
  215. }
  216. }
  217. /// <summary>
  218. /// 当InputType为数字类型时,能输入的最小值
  219. /// </summary>
  220. /// <value>The length of the decimal.</value>
  221. [Description("当InputType为数字类型时,小数位数。")]
  222. public int DecLength
  223. {
  224. get
  225. {
  226. return this._decLength;
  227. }
  228. set
  229. {
  230. this._decLength = value;
  231. }
  232. }
  233. /// <summary>
  234. /// Initializes a new instance of the <see cref="TextBoxEx" /> class.
  235. /// </summary>
  236. public TextBoxEx()
  237. {
  238. this.InitializeComponent();
  239. base.GotFocus += new EventHandler(this.TextBoxEx_GotFocus);
  240. base.MouseUp += new MouseEventHandler(this.TextBoxEx_MouseUp);
  241. base.KeyPress += TextBoxEx_KeyPress;
  242. }
  243. /// <summary>
  244. /// Handles the KeyPress event of the TextBoxEx control.
  245. /// </summary>
  246. /// <param name="sender">The source of the event.</param>
  247. /// <param name="e">The <see cref="KeyPressEventArgs" /> instance containing the event data.</param>
  248. void TextBoxEx_KeyPress(object sender, KeyPressEventArgs e)
  249. {
  250. //以下代码 取消按下回车或esc的“叮”声
  251. if (e.KeyChar == System.Convert.ToChar(13) || e.KeyChar == System.Convert.ToChar(27))
  252. {
  253. e.Handled = true;
  254. }
  255. }
  256. /// <summary>
  257. /// Handles the MouseUp event of the TextBoxEx control.
  258. /// </summary>
  259. /// <param name="sender">The source of the event.</param>
  260. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  261. private void TextBoxEx_MouseUp(object sender, MouseEventArgs e)
  262. {
  263. if (this.blnFocus)
  264. {
  265. base.SelectAll();
  266. this.blnFocus = false;
  267. }
  268. }
  269. /// <summary>
  270. /// Handles the GotFocus event of the TextBoxEx control.
  271. /// </summary>
  272. /// <param name="sender">The source of the event.</param>
  273. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  274. private void TextBoxEx_GotFocus(object sender, EventArgs e)
  275. {
  276. this.blnFocus = true;
  277. base.SelectAll();
  278. }
  279. /// <summary>
  280. /// Handles the TextChanged event of the TextBoxEx control.
  281. /// </summary>
  282. /// <param name="sender">The source of the event.</param>
  283. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  284. private void TextBoxEx_TextChanged(object sender, EventArgs e)
  285. {
  286. if (this.Text == "")
  287. {
  288. this.m_strOldValue = this.Text;
  289. }
  290. else if (this.m_strOldValue != this.Text)
  291. {
  292. if (!ControlHelper.CheckInputType(this.Text, this._inputType, this._maxValue, this._minValue, this._decLength, this._regexPattern))
  293. {
  294. int num = base.SelectionStart;
  295. if (this.m_strOldValue.Length < this.Text.Length)
  296. {
  297. num--;
  298. }
  299. else
  300. {
  301. num++;
  302. }
  303. base.TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
  304. this.Text = this.m_strOldValue;
  305. base.TextChanged += new EventHandler(this.TextBoxEx_TextChanged);
  306. if (num < 0)
  307. {
  308. num = 0;
  309. }
  310. base.SelectionStart = num;
  311. }
  312. else
  313. {
  314. this.m_strOldValue = this.Text;
  315. }
  316. }
  317. }
  318. /// <summary>
  319. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  320. /// </summary>
  321. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  322. protected override void OnPaint(PaintEventArgs e)
  323. {
  324. base.OnPaint(e);
  325. if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(this._promptText))
  326. {
  327. if (e == null)
  328. {
  329. using (Graphics graphics = Graphics.FromHwnd(base.Handle))
  330. {
  331. if (this.Text.Length == 0 && !string.IsNullOrEmpty(this.PromptText))
  332. {
  333. TextFormatFlags textFormatFlags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
  334. if (this.RightToLeft == RightToLeft.Yes)
  335. {
  336. textFormatFlags |= (TextFormatFlags.Right | TextFormatFlags.RightToLeft);
  337. }
  338. TextRenderer.DrawText(graphics, this.PromptText, this._promptFont, base.ClientRectangle, this._promptColor, textFormatFlags);
  339. }
  340. }
  341. }
  342. }
  343. }
  344. /// <summary>
  345. /// 处理 Windows 消息。
  346. /// </summary>
  347. /// <param name="m">一个 Windows 消息对象。</param>
  348. protected override void WndProc(ref Message m)
  349. {
  350. base.WndProc(ref m);
  351. if (m.Msg == 15 || m.Msg == 7 || m.Msg == 8)
  352. {
  353. this.OnPaint(null);
  354. //Invalidate();
  355. }
  356. }
  357. /// <summary>
  358. /// Handles the <see cref="E:TextChanged" /> event.
  359. /// </summary>
  360. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  361. protected override void OnTextChanged(EventArgs e)
  362. {
  363. base.OnTextChanged(e);
  364. base.Invalidate();
  365. }
  366. }
  367. }