DKTextBox.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DKTextBox.cs
  5. * 2.功能描述:扩展的文本框控件:便于修改背景颜色及字体、颜色
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System.ComponentModel;
  11. using System.Drawing;
  12. using System.Runtime.InteropServices;
  13. using System.Windows.Forms;
  14. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  15. {
  16. /// <summary>
  17. /// 扩展的文本框控件
  18. /// </summary>
  19. public partial class DKTextBox : TextBox, IDKControl
  20. {
  21. #region 成员变量
  22. /// <summary>
  23. /// 控件是否是必须输入项目
  24. /// </summary>
  25. private bool _mustInput = false;
  26. /// <summary>
  27. /// 背景色
  28. /// </summary>
  29. private Color _backgroundColor;
  30. /// <summary>
  31. /// 校验是否有错误
  32. /// </summary>
  33. private bool _hasError = false;
  34. /// <summary>
  35. /// 异步处理开始时,控件状态
  36. /// </summary>
  37. private bool _beginAsyncStatus;
  38. #endregion
  39. #region 构造函数
  40. /// <summary>
  41. /// 构造函数
  42. /// </summary>
  43. public DKTextBox()
  44. {
  45. InitializeComponent();
  46. this.Font = ControlsConst.FONT_SYSTEM_DEFAULT;
  47. this._backgroundColor = base.BackColor;
  48. this.BackColorChanged += DKTextBox_BackColorChanged;
  49. this.TextChanged += DKTextBox_TextChanged;
  50. this.ReadOnlyChanged += DKTextBox_ReadOnlyChanged;
  51. }
  52. #endregion
  53. #region 属性
  54. /// <summary>
  55. /// 获取或设置控件是否是必须输入项目
  56. /// </summary>
  57. [Description("获取或设置控件是否是必须输入项目。"), Category("CustomerEx")]
  58. [DefaultValue(false)]
  59. public bool IsMustInput
  60. {
  61. get
  62. {
  63. return this._mustInput;
  64. }
  65. set
  66. {
  67. if (_mustInput != value)
  68. {
  69. this._mustInput = value;
  70. this.SetBackColor();
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// 获取或设置控件校验时是否有错误
  76. /// </summary>
  77. [Description("获取或设置控件校验时是否有错误。"), Category("CustomerEx")]
  78. [DefaultValue(false)]
  79. public bool HasError
  80. {
  81. get
  82. {
  83. return this._hasError;
  84. }
  85. set
  86. {
  87. if (_hasError != value)
  88. {
  89. this._hasError = value;
  90. this.SetBackColor();
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 获取或设置控件的背景色
  96. /// </summary>
  97. [Description("获取或设置控件的背景色。"), Category("CustomerEx")]
  98. public Color BackgroundColor
  99. {
  100. get
  101. {
  102. return this._backgroundColor;
  103. }
  104. set
  105. {
  106. if (this._backgroundColor != value)
  107. {
  108. this._backgroundColor = value;
  109. this.SetBackColor();
  110. }
  111. }
  112. }
  113. #endregion
  114. #region 事件处理
  115. /// <summary>
  116. /// ReadOnly属性改变时改变背景色
  117. /// </summary>
  118. /// <param name="sender"></param>
  119. /// <param name="e"></param>
  120. private void DKTextBox_ReadOnlyChanged(object sender, System.EventArgs e)
  121. {
  122. this.SetBackColor();
  123. }
  124. /// <summary>
  125. /// 文本改变时改变背景色
  126. /// </summary>
  127. /// <param name="sender"></param>
  128. /// <param name="e"></param>
  129. private void DKTextBox_TextChanged(object sender, System.EventArgs e)
  130. {
  131. this.SetBackColor();
  132. }
  133. /// <summary>
  134. /// 背景色改变时
  135. /// </summary>
  136. /// <param name="sender"></param>
  137. /// <param name="e"></param>
  138. private void DKTextBox_BackColorChanged(object sender, System.EventArgs e)
  139. {
  140. this.SetBackColor();
  141. }
  142. #endregion
  143. #region 公有方法
  144. /// <summary>
  145. /// 异步处理开始
  146. /// </summary>
  147. public void BeginAsync()
  148. {
  149. this._beginAsyncStatus = this.ReadOnly;
  150. this.ReadOnly = true;
  151. }
  152. /// <summary>
  153. /// 异步处理结束
  154. /// </summary>
  155. public void EndAsync()
  156. {
  157. this.ReadOnly = this._beginAsyncStatus;
  158. }
  159. #endregion
  160. #region 私有方法
  161. /// <summary>
  162. /// 设置背景色
  163. /// </summary>
  164. private void SetBackColor()
  165. {
  166. // 项目为必须输入项时,需要修改背景颜色
  167. this.BackColorChanged -= DKTextBox_BackColorChanged;
  168. if (this.ReadOnly)
  169. {
  170. base.BackColor = ControlsConst.BACKCOLOR_TEXT_READONLY_EMPT;
  171. }
  172. else if (this._mustInput && string.IsNullOrEmpty(this.Text))
  173. {
  174. base.BackColor = ControlsConst.BACKCOLOR_TEXT_MUSTINPUT_EMPT;
  175. }
  176. else if (this._hasError)
  177. {
  178. base.BackColor = ControlsConst.BACKCOLOR_TEXT_ERROR;
  179. }
  180. else
  181. {
  182. base.BackColor = this._backgroundColor;
  183. }
  184. this.BackColorChanged -= DKTextBox_BackColorChanged;
  185. }
  186. #endregion
  187. }
  188. }