| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- /*******************************************************************************
- * 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
- {
- /// <summary>
- /// 扩展的文本框控件
- /// </summary>
- 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 属性
- /// <summary>
- /// 获取或设置限制输入的字符
- /// </summary>
- [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);
- }
- }
- }
- /// <summary>
- /// 获取或设置允许输入的字符
- /// </summary>
- [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);
- }
- }
- }
- /// <summary>
- /// 获取或设置允许输入的最小长度
- /// </summary>
- [Description("获取或设置允许输入的最小长度"), Category("CustomerEx")]
- [DefaultValue(0)]
- public int MinLength
- {
- get
- {
- return this._minLength;
- }
- set
- {
- if (this._minLength != value)
- {
- this._minLength = value;
- this.SetText(this.Text);
- }
- }
- }
- /// <summary>
- /// 获取校验未通过时的错误消息
- /// </summary>
- [Description("获取校验未通过时的错误消息"), Category("CustomerEx")]
- [DefaultValue((string)null)]
- public string ErrorMessage
- {
- get
- {
- return this._errorMessage;
- }
- set
- {
- this._errorMessage = value;
- }
- }
- /// <summary>
- /// 获取或设置文本格式的要求信息
- /// </summary>
- [Description("获取或设置文本格式的要求信息"), Category("CustomerEx")]
- [DefaultValue((string)null)]
- protected virtual string FixPatternMessage
- {
- get
- {
- return this._fixPatternMessage;
- }
- set
- {
- if (!this._allowCharsPattern)
- {
- return;
- }
- this._fixPatternMessage = value;
- }
- }
- /// <summary>
- /// 获取或设置文本格式化信息
- /// </summary>
- [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);
- }
- }
- }
- /// <summary>
- /// 获取通过校验的文本
- /// </summary>
- [Description("获取通过校验的文本"), Category("CustomerEx")]
- [DefaultValue((string)null)]
- public virtual string TextValue
- {
- get
- {
- return this._textValue;
- }
- set
- {
- this._textValue = value;
- }
- }
- /// <summary>
- /// 获取或设置文本是否可以为空(无效)
- /// </summary>
- [Description("获取或设置文本是否可以为空(无效)"), Category("CustomerEx")]
- [DefaultValue(false)]
- public virtual bool TextNullable
- {
- get
- {
- return this._textNullable;
- }
- set
- {
- this._textNullable = value;
- }
- }
- ///// <summary>
- ///// 获取或设置文本
- ///// </summary>
- //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 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public DKTextBoxBase()
- {
- InitializeComponent();
- base.Text = string.Empty;
- this.ContinueWndProc = false;
- }
- #endregion
- #region 受保护的方法/函数
- /// <summary>
- /// 限制输入文本
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- protected virtual string RejectChar(string text)
- {
- if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(this._rejectCharsPattern))
- {
- return text;
- }
- return Regex.Replace(text, this._rejectCharsPattern, string.Empty);
- }
- /// <summary>
- /// 校验输入文本
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 设置文本
- /// </summary>
- /// <param name="text"></param>
- 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;
- }
- /// <summary>
- /// 限制文本长度
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- 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);
- }
- }
- /// <summary>
- /// </remarks>
- /// <param name="message"></param>
- 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 事件
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- 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;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- 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));
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- protected virtual void VerifyText()
- {
- this.SetText(this.Text);
- }
- protected override void OnLeave(EventArgs e)
- {
- //this.VerifyText();
- base.OnLeave(e);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValidating(CancelEventArgs e)
- {
- //this.VerifyText();
- base.OnValidating(e);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- 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
- }
- }
|