| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:DKTextBox.cs
- * 2.功能描述:扩展的文本框控件:便于修改背景颜色及字体、颜色
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/08/13 1.00 新建
- *******************************************************************************/
- using System.ComponentModel;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace Dongke.IBOSS.PRD.Basics.BaseControls
- {
- /// <summary>
- /// 扩展的文本框控件
- /// </summary>
- public partial class DKTextBox : TextBox, IDKControl
- {
- #region 成员变量
- /// <summary>
- /// 控件是否是必须输入项目
- /// </summary>
- private bool _mustInput = false;
- /// <summary>
- /// 背景色
- /// </summary>
- private Color _backgroundColor;
- /// <summary>
- /// 校验是否有错误
- /// </summary>
- private bool _hasError = false;
- /// <summary>
- /// 异步处理开始时,控件状态
- /// </summary>
- private bool _beginAsyncStatus;
- #endregion
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public DKTextBox()
- {
- InitializeComponent();
- this.Font = ControlsConst.FONT_SYSTEM_DEFAULT;
- this._backgroundColor = base.BackColor;
- this.BackColorChanged += DKTextBox_BackColorChanged;
- this.TextChanged += DKTextBox_TextChanged;
- this.ReadOnlyChanged += DKTextBox_ReadOnlyChanged;
- }
- #endregion
- #region 属性
- /// <summary>
- /// 获取或设置控件是否是必须输入项目
- /// </summary>
- [Description("获取或设置控件是否是必须输入项目。"), Category("CustomerEx")]
- [DefaultValue(false)]
- public bool IsMustInput
- {
- get
- {
- return this._mustInput;
- }
- set
- {
- if (_mustInput != value)
- {
- this._mustInput = value;
- this.SetBackColor();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件校验时是否有错误
- /// </summary>
- [Description("获取或设置控件校验时是否有错误。"), Category("CustomerEx")]
- [DefaultValue(false)]
- public bool HasError
- {
- get
- {
- return this._hasError;
- }
- set
- {
- if (_hasError != value)
- {
- this._hasError = value;
- this.SetBackColor();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件的背景色
- /// </summary>
- [Description("获取或设置控件的背景色。"), Category("CustomerEx")]
- public Color BackgroundColor
- {
- get
- {
- return this._backgroundColor;
- }
- set
- {
- if (this._backgroundColor != value)
- {
- this._backgroundColor = value;
- this.SetBackColor();
- }
- }
- }
- #endregion
- #region 事件处理
- /// <summary>
- /// ReadOnly属性改变时改变背景色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKTextBox_ReadOnlyChanged(object sender, System.EventArgs e)
- {
- this.SetBackColor();
- }
- /// <summary>
- /// 文本改变时改变背景色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKTextBox_TextChanged(object sender, System.EventArgs e)
- {
- this.SetBackColor();
- }
- /// <summary>
- /// 背景色改变时
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKTextBox_BackColorChanged(object sender, System.EventArgs e)
- {
- this.SetBackColor();
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 异步处理开始
- /// </summary>
- public void BeginAsync()
- {
- this._beginAsyncStatus = this.ReadOnly;
- this.ReadOnly = true;
- }
- /// <summary>
- /// 异步处理结束
- /// </summary>
- public void EndAsync()
- {
- this.ReadOnly = this._beginAsyncStatus;
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 设置背景色
- /// </summary>
- private void SetBackColor()
- {
- // 项目为必须输入项时,需要修改背景颜色
- this.BackColorChanged -= DKTextBox_BackColorChanged;
- if (this.ReadOnly)
- {
- base.BackColor = ControlsConst.BACKCOLOR_TEXT_READONLY_EMPT;
- }
- else if (this._mustInput && string.IsNullOrEmpty(this.Text))
- {
- base.BackColor = ControlsConst.BACKCOLOR_TEXT_MUSTINPUT_EMPT;
- }
- else if (this._hasError)
- {
- base.BackColor = ControlsConst.BACKCOLOR_TEXT_ERROR;
- }
- else
- {
- base.BackColor = this._backgroundColor;
- }
- this.BackColorChanged -= DKTextBox_BackColorChanged;
- }
- #endregion
- }
- }
|