DKComboBox.cs 5.3 KB

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