DKTextBoxBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:C_TextBox.cs
  5. * 2.功能描述:扩展的文本框控件:便于修改背景颜色及字体、颜色
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.Text.RegularExpressions;
  13. using System.Windows.Forms;
  14. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  15. {
  16. /// <summary>
  17. /// 扩展的文本框控件
  18. /// </summary>
  19. public partial class DKTextBoxBase : DKTextBox
  20. {
  21. #region 成员变量
  22. private string _rejectCharsPattern;
  23. private string _fixCharsPattern;
  24. private int _minLength = 0;
  25. private string _errorMessage;
  26. private string _fixPatternMessage;
  27. private string _textValue = null;
  28. private string _textFormat = null;
  29. private bool _textNullable = false;
  30. private bool _allowCharsPattern = true;
  31. #endregion
  32. #region 属性
  33. /// <summary>
  34. /// 获取或设置限制输入的字符
  35. /// </summary>
  36. [Description("获取或设置限制输入的字符"), Category("CustomerEx")]
  37. [DefaultValue((string)null)]
  38. protected virtual string RejectChars
  39. {
  40. get
  41. {
  42. return this._rejectCharsPattern;
  43. }
  44. set
  45. {
  46. if (!this._allowCharsPattern)
  47. {
  48. return;
  49. }
  50. if (this._rejectCharsPattern != value)
  51. {
  52. this._rejectCharsPattern = value;
  53. this.SetText(this.Text);
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// 获取或设置允许输入的字符
  59. /// </summary>
  60. [Description("获取或设置允许输入的字符"), Category("CustomerEx")]
  61. [DefaultValue((string)null)]
  62. protected virtual string FixChars
  63. {
  64. get
  65. {
  66. return this._fixCharsPattern;
  67. }
  68. set
  69. {
  70. if (!this._allowCharsPattern)
  71. {
  72. return;
  73. }
  74. if (this._fixCharsPattern != value)
  75. {
  76. this._fixCharsPattern = value;
  77. this.SetText(this.Text);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 获取或设置允许输入的最小长度
  83. /// </summary>
  84. [Description("获取或设置允许输入的最小长度"), Category("CustomerEx")]
  85. [DefaultValue(0)]
  86. public int MinLength
  87. {
  88. get
  89. {
  90. return this._minLength;
  91. }
  92. set
  93. {
  94. if (this._minLength != value)
  95. {
  96. this._minLength = value;
  97. this.SetText(this.Text);
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// 获取校验未通过时的错误消息
  103. /// </summary>
  104. [Description("获取校验未通过时的错误消息"), Category("CustomerEx")]
  105. [DefaultValue((string)null)]
  106. public string ErrorMessage
  107. {
  108. get
  109. {
  110. return this._errorMessage;
  111. }
  112. set
  113. {
  114. this._errorMessage = value;
  115. }
  116. }
  117. /// <summary>
  118. /// 获取或设置文本格式的要求信息
  119. /// </summary>
  120. [Description("获取或设置文本格式的要求信息"), Category("CustomerEx")]
  121. [DefaultValue((string)null)]
  122. protected virtual string FixPatternMessage
  123. {
  124. get
  125. {
  126. return this._fixPatternMessage;
  127. }
  128. set
  129. {
  130. if (!this._allowCharsPattern)
  131. {
  132. return;
  133. }
  134. this._fixPatternMessage = value;
  135. }
  136. }
  137. /// <summary>
  138. /// 获取或设置文本格式化信息
  139. /// </summary>
  140. [Description("获取或设置文本格式化信息"), Category("CustomerEx")]
  141. [DefaultValue((string)null)]
  142. public virtual string TextFormat
  143. {
  144. get
  145. {
  146. return this._textFormat;
  147. }
  148. set
  149. {
  150. if (this._textFormat == value)
  151. {
  152. this._textFormat = value;
  153. this.SetText(this.Text);
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// 获取通过校验的文本
  159. /// </summary>
  160. [Description("获取通过校验的文本"), Category("CustomerEx")]
  161. [DefaultValue((string)null)]
  162. public virtual string TextValue
  163. {
  164. get
  165. {
  166. return this._textValue;
  167. }
  168. set
  169. {
  170. this._textValue = value;
  171. }
  172. }
  173. /// <summary>
  174. /// 获取或设置文本是否可以为空(无效)
  175. /// </summary>
  176. [Description("获取或设置文本是否可以为空(无效)"), Category("CustomerEx")]
  177. [DefaultValue(false)]
  178. public virtual bool TextNullable
  179. {
  180. get
  181. {
  182. return this._textNullable;
  183. }
  184. set
  185. {
  186. this._textNullable = value;
  187. }
  188. }
  189. ///// <summary>
  190. ///// 获取或设置文本
  191. ///// </summary>
  192. //public new string Text
  193. //{
  194. // get
  195. // {
  196. // return base.Text;
  197. // }
  198. // set
  199. // {
  200. // if (base.Text != value)
  201. // {
  202. // this.SetText(value);
  203. // }
  204. // }
  205. //}
  206. protected virtual bool ContinueWndProc
  207. {
  208. get;
  209. set;
  210. }
  211. protected virtual bool IsSetText
  212. {
  213. get;
  214. set;
  215. }
  216. protected virtual bool AllowCharsPattern
  217. {
  218. get
  219. {
  220. return this._allowCharsPattern;
  221. }
  222. set
  223. {
  224. this._allowCharsPattern = value;
  225. }
  226. }
  227. #endregion
  228. #region 构造函数
  229. /// <summary>
  230. /// 构造函数
  231. /// </summary>
  232. public DKTextBoxBase()
  233. {
  234. InitializeComponent();
  235. base.Text = string.Empty;
  236. this.ContinueWndProc = false;
  237. }
  238. #endregion
  239. #region 受保护的方法/函数
  240. /// <summary>
  241. /// 限制输入文本
  242. /// </summary>
  243. /// <param name="text"></param>
  244. /// <returns></returns>
  245. protected virtual string RejectChar(string text)
  246. {
  247. if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(this._rejectCharsPattern))
  248. {
  249. return text;
  250. }
  251. return Regex.Replace(text, this._rejectCharsPattern, string.Empty);
  252. }
  253. /// <summary>
  254. /// 校验输入文本
  255. /// </summary>
  256. /// <param name="text"></param>
  257. /// <returns></returns>
  258. protected virtual bool CheckText(string text)
  259. {
  260. if (string.IsNullOrEmpty(text))
  261. {
  262. if (this.IsMustInput)
  263. {
  264. this.HasError = true;
  265. this.ErrorMessage = "必须输入项目";
  266. return false;
  267. }
  268. this.HasError = false;
  269. this.ErrorMessage = string.Empty;
  270. return true;
  271. }
  272. if (0 < this._minLength && text.Length < this._minLength)
  273. {
  274. this.HasError = true;
  275. this.ErrorMessage = "文本小于最小长度" + this.MinLength;
  276. return false;
  277. }
  278. if (!string.IsNullOrEmpty(this._fixCharsPattern))
  279. {
  280. if (!Regex.IsMatch(text, this._fixCharsPattern))
  281. {
  282. this.HasError = true;
  283. this.ErrorMessage = this._fixPatternMessage;
  284. return false;
  285. }
  286. }
  287. this.HasError = false;
  288. this.ErrorMessage = string.Empty;
  289. return true;
  290. }
  291. /// <summary>
  292. /// 设置文本
  293. /// </summary>
  294. /// <param name="text"></param>
  295. protected virtual bool SetText(string text)
  296. {
  297. this._textValue = null;
  298. text = this.RejectChar(text);
  299. if (this.CheckText(text))
  300. {
  301. if (!string.IsNullOrEmpty(this._textFormat) && !string.IsNullOrEmpty(text))
  302. {
  303. text = string.Format("{0:" + this._textFormat + "}", text);
  304. }
  305. this._textValue = text;
  306. }
  307. if (base.Text != text)
  308. {
  309. this.IsSetText = true;
  310. base.Text = text;
  311. return true;
  312. }
  313. return true;
  314. }
  315. /// <summary>
  316. /// 限制文本长度
  317. /// </summary>
  318. /// <param name="input"></param>
  319. /// <returns></returns>
  320. protected string LimitLength(string input)
  321. {
  322. if (this.MaxLength <= 0 || string.IsNullOrEmpty(input))
  323. {
  324. return input;
  325. }
  326. int textCount = this.Text.Length;
  327. int inputCount = input.Length;
  328. int selectedTextCount = 0;
  329. if (!string.IsNullOrEmpty(this.Text))
  330. {
  331. selectedTextCount = this.SelectionLength;
  332. }
  333. // 可以输入的文本长度
  334. int remainCount = this.MaxLength - (textCount - selectedTextCount);
  335. if (remainCount <= 0)
  336. {
  337. return null;
  338. }
  339. if (inputCount <= remainCount)
  340. {
  341. return input;
  342. }
  343. else
  344. {
  345. return input.Substring(0, remainCount);
  346. }
  347. }
  348. /// <summary>
  349. /// </remarks>
  350. /// <param name="message"></param>
  351. protected override void WndProc(ref Message message)
  352. {
  353. const int WM_CHAR = 0x0102;
  354. const int WM_PASTE = 0x0302;
  355. if (message.Msg == WM_CHAR)
  356. {
  357. KeyPressEventArgs eKeyPress =
  358. new KeyPressEventArgs((Char)(message.WParam.ToInt32()));
  359. this.OnChar(eKeyPress);
  360. if (eKeyPress.Handled)
  361. {
  362. if (!this.ContinueWndProc)
  363. {
  364. return;
  365. }
  366. }
  367. }
  368. else if (message.Msg == WM_PASTE)
  369. {
  370. this.OnPaste(EventArgs.Empty);
  371. if (!this.ContinueWndProc)
  372. {
  373. return;
  374. }
  375. }
  376. base.WndProc(ref message);
  377. }
  378. #endregion
  379. #region 事件
  380. /// <summary>
  381. ///
  382. /// </summary>
  383. /// <param name="e"></param>
  384. protected virtual void OnChar(KeyPressEventArgs e)
  385. {
  386. if (this.Enabled == false || this.ReadOnly || char.IsControl(e.KeyChar))
  387. {
  388. return;
  389. }
  390. string text = e.KeyChar.ToString();
  391. this.SelectedText = this.LimitLength(this.RejectChar(text));
  392. e.Handled = true;
  393. }
  394. /// <summary>
  395. ///
  396. /// </summary>
  397. /// <param name="e"></param>
  398. protected virtual void OnPaste(EventArgs e)
  399. {
  400. if (this.Enabled == false || this.ReadOnly)
  401. {
  402. return;
  403. }
  404. object clipboardText =
  405. Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.UnicodeText);
  406. if (clipboardText == null)
  407. {
  408. clipboardText =
  409. Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.Text);
  410. if (clipboardText == null)
  411. {
  412. return;
  413. }
  414. }
  415. string text = clipboardText.ToString();
  416. this.SelectedText = this.LimitLength(this.RejectChar(text));
  417. }
  418. /// <summary>
  419. ///
  420. /// </summary>
  421. /// <param name="e"></param>
  422. protected virtual void VerifyText()
  423. {
  424. this.SetText(this.Text);
  425. }
  426. protected override void OnLeave(EventArgs e)
  427. {
  428. //this.VerifyText();
  429. base.OnLeave(e);
  430. }
  431. /// <summary>
  432. ///
  433. /// </summary>
  434. /// <param name="e"></param>
  435. protected override void OnValidating(CancelEventArgs e)
  436. {
  437. //this.VerifyText();
  438. base.OnValidating(e);
  439. }
  440. /// <summary>
  441. ///
  442. /// </summary>
  443. /// <param name="e"></param>
  444. protected override void OnTextChanged(EventArgs e)
  445. {
  446. if (!this.IsSetText)
  447. {
  448. if (this.SetText(this.Text))
  449. {
  450. base.OnTextChanged(e);
  451. }
  452. }
  453. else
  454. {
  455. base.OnTextChanged(e);
  456. }
  457. this.IsSetText = false;
  458. }
  459. #endregion
  460. }
  461. }