/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:C_TextBox.cs
* 2.功能描述:扩展的文本框控件:便于修改背景颜色及字体、颜色
* 编辑履历:
* 作者 日期 版本 修改内容
* 陈晓野 2014/08/13 1.00 新建
*******************************************************************************/
using System;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Dongke.IBOSS.PRD.Basics.BaseControls
{
///
/// 扩展的文本框控件
///
public partial class DKTextBoxBase : DKTextBox
{
#region 成员变量
private string _rejectCharsPattern;
private string _fixCharsPattern;
private int _minLength = 0;
private string _errorMessage;
private string _fixPatternMessage;
private string _textValue = null;
private string _textFormat = null;
private bool _textNullable = false;
private bool _allowCharsPattern = true;
#endregion
#region 属性
///
/// 获取或设置限制输入的字符
///
[Description("获取或设置限制输入的字符"), Category("CustomerEx")]
[DefaultValue((string)null)]
protected virtual string RejectChars
{
get
{
return this._rejectCharsPattern;
}
set
{
if (!this._allowCharsPattern)
{
return;
}
if (this._rejectCharsPattern != value)
{
this._rejectCharsPattern = value;
this.SetText(this.Text);
}
}
}
///
/// 获取或设置允许输入的字符
///
[Description("获取或设置允许输入的字符"), Category("CustomerEx")]
[DefaultValue((string)null)]
protected virtual string FixChars
{
get
{
return this._fixCharsPattern;
}
set
{
if (!this._allowCharsPattern)
{
return;
}
if (this._fixCharsPattern != value)
{
this._fixCharsPattern = value;
this.SetText(this.Text);
}
}
}
///
/// 获取或设置允许输入的最小长度
///
[Description("获取或设置允许输入的最小长度"), Category("CustomerEx")]
[DefaultValue(0)]
public int MinLength
{
get
{
return this._minLength;
}
set
{
if (this._minLength != value)
{
this._minLength = value;
this.SetText(this.Text);
}
}
}
///
/// 获取校验未通过时的错误消息
///
[Description("获取校验未通过时的错误消息"), Category("CustomerEx")]
[DefaultValue((string)null)]
public string ErrorMessage
{
get
{
return this._errorMessage;
}
set
{
this._errorMessage = value;
}
}
///
/// 获取或设置文本格式的要求信息
///
[Description("获取或设置文本格式的要求信息"), Category("CustomerEx")]
[DefaultValue((string)null)]
protected virtual string FixPatternMessage
{
get
{
return this._fixPatternMessage;
}
set
{
if (!this._allowCharsPattern)
{
return;
}
this._fixPatternMessage = value;
}
}
///
/// 获取或设置文本格式化信息
///
[Description("获取或设置文本格式化信息"), Category("CustomerEx")]
[DefaultValue((string)null)]
public virtual string TextFormat
{
get
{
return this._textFormat;
}
set
{
if (this._textFormat == value)
{
this._textFormat = value;
this.SetText(this.Text);
}
}
}
///
/// 获取通过校验的文本
///
[Description("获取通过校验的文本"), Category("CustomerEx")]
[DefaultValue((string)null)]
public virtual string TextValue
{
get
{
return this._textValue;
}
set
{
this._textValue = value;
}
}
///
/// 获取或设置文本是否可以为空(无效)
///
[Description("获取或设置文本是否可以为空(无效)"), Category("CustomerEx")]
[DefaultValue(false)]
public virtual bool TextNullable
{
get
{
return this._textNullable;
}
set
{
this._textNullable = value;
}
}
/////
///// 获取或设置文本
/////
//public new string Text
//{
// get
// {
// return base.Text;
// }
// set
// {
// if (base.Text != value)
// {
// this.SetText(value);
// }
// }
//}
protected virtual bool ContinueWndProc
{
get;
set;
}
protected virtual bool IsSetText
{
get;
set;
}
protected virtual bool AllowCharsPattern
{
get
{
return this._allowCharsPattern;
}
set
{
this._allowCharsPattern = value;
}
}
#endregion
#region 构造函数
///
/// 构造函数
///
public DKTextBoxBase()
{
InitializeComponent();
base.Text = string.Empty;
this.ContinueWndProc = false;
}
#endregion
#region 受保护的方法/函数
///
/// 限制输入文本
///
///
///
protected virtual string RejectChar(string text)
{
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(this._rejectCharsPattern))
{
return text;
}
return Regex.Replace(text, this._rejectCharsPattern, string.Empty);
}
///
/// 校验输入文本
///
///
///
protected virtual bool CheckText(string text)
{
if (string.IsNullOrEmpty(text))
{
if (this.IsMustInput)
{
this.HasError = true;
this.ErrorMessage = "必须输入项目";
return false;
}
this.HasError = false;
this.ErrorMessage = string.Empty;
return true;
}
if (0 < this._minLength && text.Length < this._minLength)
{
this.HasError = true;
this.ErrorMessage = "文本小于最小长度" + this.MinLength;
return false;
}
if (!string.IsNullOrEmpty(this._fixCharsPattern))
{
if (!Regex.IsMatch(text, this._fixCharsPattern))
{
this.HasError = true;
this.ErrorMessage = this._fixPatternMessage;
return false;
}
}
this.HasError = false;
this.ErrorMessage = string.Empty;
return true;
}
///
/// 设置文本
///
///
protected virtual bool SetText(string text)
{
this._textValue = null;
text = this.RejectChar(text);
if (this.CheckText(text))
{
if (!string.IsNullOrEmpty(this._textFormat) && !string.IsNullOrEmpty(text))
{
text = string.Format("{0:" + this._textFormat + "}", text);
}
this._textValue = text;
}
if (base.Text != text)
{
this.IsSetText = true;
base.Text = text;
return true;
}
return true;
}
///
/// 限制文本长度
///
///
///
protected string LimitLength(string input)
{
if (this.MaxLength <= 0 || string.IsNullOrEmpty(input))
{
return input;
}
int textCount = this.Text.Length;
int inputCount = input.Length;
int selectedTextCount = 0;
if (!string.IsNullOrEmpty(this.Text))
{
selectedTextCount = this.SelectionLength;
}
// 可以输入的文本长度
int remainCount = this.MaxLength - (textCount - selectedTextCount);
if (remainCount <= 0)
{
return null;
}
if (inputCount <= remainCount)
{
return input;
}
else
{
return input.Substring(0, remainCount);
}
}
///
///
///
protected override void WndProc(ref Message message)
{
const int WM_CHAR = 0x0102;
const int WM_PASTE = 0x0302;
if (message.Msg == WM_CHAR)
{
KeyPressEventArgs eKeyPress =
new KeyPressEventArgs((Char)(message.WParam.ToInt32()));
this.OnChar(eKeyPress);
if (eKeyPress.Handled)
{
if (!this.ContinueWndProc)
{
return;
}
}
}
else if (message.Msg == WM_PASTE)
{
this.OnPaste(EventArgs.Empty);
if (!this.ContinueWndProc)
{
return;
}
}
base.WndProc(ref message);
}
#endregion
#region 事件
///
///
///
///
protected virtual void OnChar(KeyPressEventArgs e)
{
if (this.Enabled == false || this.ReadOnly || char.IsControl(e.KeyChar))
{
return;
}
string text = e.KeyChar.ToString();
this.SelectedText = this.LimitLength(this.RejectChar(text));
e.Handled = true;
}
///
///
///
///
protected virtual void OnPaste(EventArgs e)
{
if (this.Enabled == false || this.ReadOnly)
{
return;
}
object clipboardText =
Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.UnicodeText);
if (clipboardText == null)
{
clipboardText =
Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.Text);
if (clipboardText == null)
{
return;
}
}
string text = clipboardText.ToString();
this.SelectedText = this.LimitLength(this.RejectChar(text));
}
///
///
///
///
protected virtual void VerifyText()
{
this.SetText(this.Text);
}
protected override void OnLeave(EventArgs e)
{
//this.VerifyText();
base.OnLeave(e);
}
///
///
///
///
protected override void OnValidating(CancelEventArgs e)
{
//this.VerifyText();
base.OnValidating(e);
}
///
///
///
///
protected override void OnTextChanged(EventArgs e)
{
if (!this.IsSetText)
{
if (this.SetText(this.Text))
{
base.OnTextChanged(e);
}
}
else
{
base.OnTextChanged(e);
}
this.IsSetText = false;
}
#endregion
}
}