/*******************************************************************************
* 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
{
///
/// 扩展的文本框控件
///
public partial class DKTextBox : TextBox, IDKControl
{
#region 成员变量
///
/// 控件是否是必须输入项目
///
private bool _mustInput = false;
///
/// 背景色
///
private Color _backgroundColor;
///
/// 校验是否有错误
///
private bool _hasError = false;
///
/// 异步处理开始时,控件状态
///
private bool _beginAsyncStatus;
#endregion
#region 构造函数
///
/// 构造函数
///
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 属性
///
/// 获取或设置控件是否是必须输入项目
///
[Description("获取或设置控件是否是必须输入项目。"), Category("CustomerEx")]
[DefaultValue(false)]
public bool IsMustInput
{
get
{
return this._mustInput;
}
set
{
if (_mustInput != value)
{
this._mustInput = value;
this.SetBackColor();
}
}
}
///
/// 获取或设置控件校验时是否有错误
///
[Description("获取或设置控件校验时是否有错误。"), Category("CustomerEx")]
[DefaultValue(false)]
public bool HasError
{
get
{
return this._hasError;
}
set
{
if (_hasError != value)
{
this._hasError = value;
this.SetBackColor();
}
}
}
///
/// 获取或设置控件的背景色
///
[Description("获取或设置控件的背景色。"), Category("CustomerEx")]
public Color BackgroundColor
{
get
{
return this._backgroundColor;
}
set
{
if (this._backgroundColor != value)
{
this._backgroundColor = value;
this.SetBackColor();
}
}
}
#endregion
#region 事件处理
///
/// ReadOnly属性改变时改变背景色
///
///
///
private void DKTextBox_ReadOnlyChanged(object sender, System.EventArgs e)
{
this.SetBackColor();
}
///
/// 文本改变时改变背景色
///
///
///
private void DKTextBox_TextChanged(object sender, System.EventArgs e)
{
this.SetBackColor();
}
///
/// 背景色改变时
///
///
///
private void DKTextBox_BackColorChanged(object sender, System.EventArgs e)
{
this.SetBackColor();
}
#endregion
#region 公有方法
///
/// 异步处理开始
///
public void BeginAsync()
{
this._beginAsyncStatus = this.ReadOnly;
this.ReadOnly = true;
}
///
/// 异步处理结束
///
public void EndAsync()
{
this.ReadOnly = this._beginAsyncStatus;
}
#endregion
#region 私有方法
///
/// 设置背景色
///
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
}
}