| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362 |
-
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using Dongke.WinForm.Utilities;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 限制输入文本框控件
- /// </summary>
- public abstract class TextBoxRejected : TextBox, IDKControl,
- IDataVerifiable, IAsyncControl
- {
- #region 事件声明
- /*
- #region TextPaste
- /// <summary>
- /// 当输入文本粘贴后发生
- /// </summary>
- private static readonly object EventTextPaste = new object();
- /// <summary>
- /// TextPaste 事件
- /// </summary>
- public event TextPasteEventHandler TextPaste
- {
- add
- {
- base.Events.AddHandler(TextBoxRejected.EventTextPaste, value);
- }
- remove
- {
- base.Events.RemoveHandler(TextBoxRejected.EventTextPaste, value);
- }
- }
- #endregion
- #region EditReadOnlyChanged
- /// <summary>
- /// 当 EditReadOnly 属性的值更改时发生。
- /// </summary>
- private static readonly object EventEditReadOnlyChanged = new object();
- /// <summary>
- /// 当 EditReadOnly 属性的值更改时发生
- /// </summary>
- [Description("当 EditReadOnly 属性的值更改时发生。"), Category("CustomerEx")]
- public event EventHandler EditReadOnlyChanged
- {
- add
- {
- base.Events.AddHandler(EventEditReadOnlyChanged, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventEditReadOnlyChanged, value);
- }
- }
- /// <summary>
- /// 引发 EditReadOnlyChanged 事件
- /// </summary>
- /// <param name="e"></param>
- protected virtual void OnEditReadOnlyChanged(EventArgs e)
- {
- EventHandler eventHandler = base.Events[EventEditReadOnlyChanged] as EventHandler;
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- #endregion
- */
- #region HasErrorChanged
- /// <summary>
- /// 当 HasError 属性的值更改时发生。
- /// </summary>
- private static readonly object EventHasErrorChanged = new object();
- /// <summary>
- /// 当 HasError 属性的值更改时发生。
- /// </summary>
- [Description("当 HasError 属性的值更改时发生。"), Category("CustomerEx")]
- public event EventHandler HasErrorChanged
- {
- add
- {
- base.Events.AddHandler(EventHasErrorChanged, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventHasErrorChanged, value);
- }
- }
- #endregion
- #endregion
- #region 成员变量
- ///// <summary>
- ///// 指示控件中的文本是否为只读
- ///// </summary>
- //private bool _editReadOnly = true;
- /// <summary>
- /// 验证不通过,焦点不离开
- /// </summary>
- private bool _validatingCancel = false;
- /// <summary>
- /// 焦点是否进入控件
- /// </summary>
- private bool _entered = false;
- /// <summary>
- /// 鼠标是否进入控件
- /// </summary>
- private bool _mouseOver = false;
- /// <summary>
- /// 边框颜色
- /// </summary>
- private Color? _borderColor = null;
- /// <summary>
- /// 拒绝输入的字符
- /// </summary>
- private string _rejected = null;
- /// <summary>
- /// 限定正则表达式
- /// </summary>
- private string _regular = null;
- /// <summary>
- /// 限制最小长度
- /// </summary>
- private int _minLength = 0;
- /// <summary>
- /// Unicode 填充字符,来达到指定的允许输入的最小长度
- /// </summary>
- private char? _paddingChar = null;
- /// <summary>
- /// 填充指定的 Unicode 字符来达到指定的总长度,从而使这些字符对齐方式。
- /// </summary>
- private TextPadStyle _textPad = TextPadStyle.Left;
- /// <summary>
- /// 控件在获得焦点时,文本内容的选择方式。
- /// </summary>
- private EnteredSelectTextStyle _enteredSelectTextStyle = EnteredSelectTextStyle.None;
- /// <summary>
- /// 符合规则的文本
- /// </summary>
- protected string _textValue = null;
- /// <summary>
- /// 正在设置文本
- /// </summary>
- protected bool _isSettingText = false;
- #endregion
- #region 构造函数
- /// <summary>
- /// 标准复选框列表控件
- /// </summary>
- public TextBoxRejected()
- {
- CommonSetting.InitControls(this);
- base.MaxLength = 1000;
- }
- #endregion
- #region 属性
- ///// <summary>
- ///// 获取或设置一个值,该值指示文本框中的文本是否为只读。
- ///// </summary>
- //[Description("获取或设置一个值,该值指示文本框中的文本是否为只读。"), Category("CustomerEx")]
- //[DefaultValue(true)]
- //public virtual bool EditReadOnly
- //{
- // get
- // {
- // return this._editReadOnly;
- // }
- // set
- // {
- // if (this._editReadOnly != value)
- // {
- // this._editReadOnly = value;
- // this.OnEditReadOnlyChanged(EventArgs.Empty);
- // }
- // }
- //}
- /// <summary>
- /// 获取一个值,该值指示控件是否有输入焦点。
- /// </summary>
- [DefaultValue(false)]
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public virtual bool Entered
- {
- get
- {
- return this._entered;
- }
- protected set
- {
- if (this._entered != value)
- {
- this._entered = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取一个值,该值指示鼠标是否在控件上方。
- /// </summary>
- [DefaultValue(false)]
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public virtual bool MouseOver
- {
- get
- {
- return this._mouseOver;
- }
- protected set
- {
- if (this._mouseOver != value)
- {
- this._mouseOver = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置允许输入的最小长度。
- /// </summary>
- [Description("获取或设置允许输入的最小长度。"), Category("CustomerEx")]
- [DefaultValue(0)]
- public virtual int MinLength
- {
- get
- {
- return Math.Min(this._minLength, this.MaxLength);
- }
- set
- {
- if (value < 0 || value > this.MaxLength)
- {
- return;
- }
- if (this._minLength != value)
- {
- if (value > this.MinLength)
- {
- this._minLength = value;
- this.SetTextValue();
- }
- else
- {
- this._minLength = value;
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置Unicode 填充字符,来达到指定的允许输入的最小长度。填充字符不受过滤限制。
- /// </summary>
- [Description("获取或设置Unicode 填充字符,来达到指定的允许输入的最小长度。填充字符不受过滤限制。"), Category("CustomerEx")]
- [DefaultValue(null)]
- public virtual char? PaddingChar
- {
- get
- {
- return this._paddingChar;
- }
- set
- {
- if (this._paddingChar != value)
- {
- if (!this._paddingChar.HasValue && value.HasValue)
- {
- this._paddingChar = value;
- this.SetTextValue();
- }
- else
- {
- this._paddingChar = value;
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置填充指定的 Unicode 字符来达到指定的总长度的对齐方式。
- /// </summary>
- [Description("获取或设置填充指定的 Unicode 字符来达到指定的总长度的对齐方式。"), Category("CustomerEx")]
- [DefaultValue(typeof(TextPadStyle), "Left")]
- public virtual TextPadStyle TextPad
- {
- get
- {
- return this._textPad;
- }
- set
- {
- if (this._textPad != value)
- {
- this._textPad = value;
- }
- }
- }
- /// <summary>
- /// 获取符合规则的文本
- /// </summary>
- [Description("获取符合规则的文本,未通过校验时为string.Empty"), Category("CustomerEx")]
- [DefaultValue((string)null)]
- public virtual string TextValue
- {
- get
- {
- return this._textValue;
- }
- }
- /// <summary>
- /// 获取或设置限制输入的字符。填充字符不受过滤限制。
- /// </summary>
- [Description("获取或设置限制输入的字符。填充字符不受过滤限制。"), Category("CustomerEx")]
- [DefaultValue((string)null)]
- protected virtual string Rejected
- {
- get
- {
- return this._rejected;
- }
- set
- {
- if (this._rejected != value)
- {
- this._rejected = value;
- this.SetTextValue();
- }
- }
- }
- /// <summary>
- /// 获取或设置输入内容应该符合的正则表达式
- /// </summary>
- [Description("获取或设置输入内容应该符合的正则表达式"), Category("CustomerEx")]
- [DefaultValue((string)null)]
- protected virtual string Regular
- {
- get
- {
- return this._regular;
- }
- set
- {
- if (this._regular != value)
- {
- this._regular = value;
- this.ValidateData();
- }
- }
- }
- ///// <summary>
- /////
- ///// </summary>
- //protected virtual bool BeginWithZero
- //{
- //}
- #endregion
- #region 重写属性
- /// <summary>
- /// 获取或设置用户可在文本框控件中键入或粘贴的最大字符数
- /// </summary>
- [DefaultValue(1000)]
- public override int MaxLength
- {
- get
- {
- return base.MaxLength;
- }
- set
- {
- base.MaxLength = value;
- if (value < this._minLength)
- {
- this.MinLength = value;
- }
- }
- }
- /// <summary>
- /// 获取或设置当前文本。
- /// </summary>
- public override string Text
- {
- get
- {
- return base.Text;
- }
- set
- {
- this.SetTextValue(value);
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示控件中当前选定的文本。
- /// </summary>
- public override string SelectedText
- {
- get
- {
- return base.SelectedText;
- }
- set
- {
- this.PasteChars(value);
- }
- }
- #endregion
- #region 重写事件
- #region 属性改变
- /// <summary>
- /// 引发 HasErrorChanged 事件
- /// </summary>
- /// <param name="e">包含事件数据的 EventArgs</param>
- protected virtual void OnHasErrorChanged(EventArgs e)
- {
- EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged];
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- /// <summary>
- /// 文本改变事件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnTextChanged(EventArgs e)
- {
- if (!this._isSettingText)
- {
- this.SetTextValue();
- return;
- }
- this._isSettingText = false;
- base.OnTextChanged(EventArgs.Empty);
- // TODO 实时验证?
- if (true)
- {
- this.ValidateData();
- }
- }
- #endregion
- #region 行为事件
- /// <summary>
- /// 引发 TextPaste 事件
- /// </summary>
- /// <param name="e">包含事件数据的 TextPasteEventArgs</param>
- protected virtual void OnPaste(TextPasteEventArgs e)
- {
- //TextPasteEventHandler eventHandler = (TextPasteEventHandler)base.Events[EventTextPaste];
- //if (eventHandler != null)
- //{
- // eventHandler(this, e);
- //}
- }
- #endregion
- #region 键盘事件
- /// <summary>
- /// 键盘输入
- /// </summary>
- /// <param name="e"></param>
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- if (!this.Enabled || this.ReadOnly || char.IsControl(e.KeyChar))
- {
- base.OnKeyPress(e);
- if (e.KeyChar == (char)8)//WindowsAPI.KeysToChar(Keys.Back))
- {
- this._isSettingText = true;
- }
- return;
- }
- //if (this._editReadOnly)
- //{
- // if (e.KeyChar == (char)8)
- // {
- // this.Text = string.Empty;
- // }
- // e.Handled = true;
- // return;
- //}
- base.OnKeyPress(e);
- if (e.Handled)
- {
- return;
- }
- string chars = this.RejectChars(e.KeyChar.ToString());
- //chars = this.LimitLength(chars);
- if (chars.Length > 0)
- {
- this._isSettingText = true;
- }
- else
- {
- WindowsAPI.MessageBeep(BeepType.MB_OK);
- e.Handled = true;
- }
- }
- /// <summary>
- /// 键盘点击
- /// </summary>
- /// <param name="e"></param>
- protected override void OnKeyDown(KeyEventArgs e)
- {
- base.OnKeyDown(e);
- if (e.Handled)
- {
- return;
- }
- if (e.KeyCode == Keys.Back ||
- e.KeyCode == Keys.Delete)
- {
- //if (this._editReadOnly)
- //{
- // this.Text = string.Empty;
- // e.Handled = true;
- // return;
- //}
- this._isSettingText = true;
- }
- }
- #endregion
- #region 焦点事件
- /// <summary>
- /// 输入焦点进入控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnEnter(EventArgs e)
- {
- this.Entered = true;
- base.OnEnter(e);
- }
- /// <summary>
- /// 获得焦点
- /// </summary>
- /// <param name="e"></param>
- protected override void OnGotFocus(EventArgs e)
- {
- base.OnGotFocus(e);
- if (this._validatingCancel)
- {
- this._validatingCancel = false;
- return;
- }
- int textLength = base.Text.Length;
- if (textLength > 0)
- {
- switch (this._enteredSelectTextStyle)
- {
- case EnteredSelectTextStyle.Left:
- this.Select(0, 0);
- break;
- case EnteredSelectTextStyle.Right:
- this.Select(textLength, 0);
- break;
- case EnteredSelectTextStyle.All:
- this.SelectAll();
- break;
- default:
- break;
- }
- }
- }
- /// <summary>
- /// 失去焦点
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- }
- /// <summary>
- /// 输入焦点离开控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLeave(EventArgs e)
- {
- base.OnLeave(e);
- //this.Entered = false;
- }
- /// <summary>
- /// 控件正在验证
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValidating(CancelEventArgs e)
- {
- base.OnValidating(e);
- if (this.HasError && !this._canLostFocusOnError)
- {
- e.Cancel = true;
- }
- this._validatingCancel = e.Cancel;
- }
- /// <summary>
- /// 控件完成验证
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValidated(EventArgs e)
- {
- this.Entered = false;
- this.SetText(this.FormatText(base.Text));
- this._validatingCancel = false;
- base.OnValidated(e);
- }
- #endregion
- #region 鼠标事件
- /// <summary>
- /// 鼠标进入控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseEnter(EventArgs e)
- {
- this.MouseOver = true;
- base.OnMouseEnter(e);
- }
- /// <summary>
- /// 鼠标离开控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- this.MouseOver = false;
- }
- /// <summary>
- /// 鼠标移动
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- }
- /// <summary>
- /// 鼠标按下
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- }
- /// <summary>
- /// 鼠标抬起
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- }
- #endregion
- #endregion
- #region 重写方法
- /// <summary>
- /// 处理 Windows 消息
- /// </summary>
- /// <param name="m">要处理的 Windows System.Windows.Forms.Message</param>
- protected override void WndProc(ref Message m)
- {
- switch (m.Msg)
- {
- case (int)WindowsMessage.WM_PASTE:
- // 粘贴
- //if (this._editReadOnly)
- //{
- // WindowsAPI.MessageBeep(BeepType.MB_OK);
- // return;
- //}
- this.PasteChars();
- return;
- case (int)WindowsMessage.WM_CUT:
- // 右键菜单 剪切
- //if (this._editReadOnly)
- //{
- // WindowsAPI.MessageBeep(BeepType.MB_OK);
- // return;
- //}
- this._isSettingText = true;
- break;
- case (int)WindowsMessage.WM_CHAR:
- if (m.WParam.ToInt32() == 18)
- {
- // Ctrl + X 18
- //if (this._editReadOnly)
- //{
- // WindowsAPI.MessageBeep(BeepType.MB_OK);
- // return;
- //}
- this._isSettingText = true;
- }
- break;
- default:
- break;
- }
- base.WndProc(ref m);
- if ((int)m.Result == 1)
- {
- // 剪切、back、delete 等操作没有改变Text时,设置 _isSettingText = false。
- switch (m.Msg)
- {
- case (int)WindowsMessage.WM_CUT:
- // 剪切空白
- case (int)WindowsMessage.WM_CHAR:
- // 输入达到最大长度,back 8
- this._isSettingText = false;
- break;
- case (int)WindowsMessage.WM_KEYDOWN:
- if (m.WParam.ToInt32() == 46)
- {
- // delete 46
- this._isSettingText = false;
- }
- break;
- default:
- break;
- }
- }
- if (m.Msg == (int)WindowsMessage.WM_PAINT ||
- m.Msg == (int)WindowsMessage.WM_NCPAINT ||
- m.Msg == (int)WindowsMessage.WM_CTLCOLOREDIT)
- {
- BorderColorPaint.WmBorderPaint(this._borderColor, this.Width, this.Height, ref m);
- }
- }
- #endregion
- #region 公有方法
- #endregion
- #region 保护方法
- #endregion
- #region 私有方法
- /// <summary>
- /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。
- /// </summary>
- private void InvalidateBorder()
- {
- Color? borderColor = BorderColorPaint.GetBorderColor(this as IDataVerifiable, this._entered, this._mouseOver);
- if (borderColor != this._borderColor)
- {
- this._borderColor = borderColor;
- if (this.Parent == null)
- {
- this.Invalidate();
- }
- else
- {
- this.Parent.Invalidate(this.Bounds, true);
- }
- }
- }
- /// <summary>
- /// 设置文本(过滤限制输入字符,并验证是否符合正则表达式)
- /// </summary>
- private void SetTextValue()
- {
- this.SetTextValue(base.Text);
- }
- /// <summary>
- /// 设置文本(过滤限制输入字符,并验证是否符合正则表达式)
- /// </summary>
- /// <param name="text">文本</param>
- private void SetTextValue(string text)
- {
- // TODO 先过滤还是先补齐?
- // 过滤输入字符
- text = this.RejectChars(text);
- // 限制输入长度
- text = this.LimitLength(text, true);
- // 格式化显示文本
- text = this.FormatText(text);
- this.SetText(text);
- }
- /// <summary>
- /// 设置文本(修改base.Text属性)
- /// </summary>
- /// <param name="text">文本</param>
- private bool SetText(string text)
- {
- string baseText = base.Text;
- if (baseText != text)
- {
- this._isSettingText = true;
- base.Text = text;
- this._isSettingText = false;
- return true;
- }
- return false;
- }
- /// <summary>
- /// 过滤限制输入的字符(OnKeyPress or 属性改变)
- /// </summary>
- /// <param name="chars">文本</param>
- /// <returns>过滤后的文本</returns>
- private string RejectChars(string chars)
- {
- if (string.IsNullOrEmpty(chars))
- {
- return chars;
- }
- if (!this.Multiline)
- {
- int indexNewLine = chars.IndexOf(Environment.NewLine);
- if (indexNewLine > -1)
- {
- chars = chars.Remove(indexNewLine);
- }
- }
- if (string.IsNullOrEmpty(this._rejected))
- {
- return chars;
- }
- chars = Regex.Replace(chars, this._rejected, string.Empty);
- return chars;
- }
- /// <summary>
- /// 格式化显示文本
- /// </summary>
- /// <param name="text">文本</param>
- /// <returns>格式化后的文本</returns>
- private string FormatText(string text)
- {
- //if (string.IsNullOrEmpty(text))
- //{
- // return text;
- //}
- // 填充字符,使文本对齐。
- if (!this._entered && this._paddingChar.HasValue)
- {
- int textLength = (text != null) ? text.Length : 0;
- int minLength = this.MinLength;
- if ((this._mustInput && minLength > 0 && textLength == 0) ||
- (textLength > 0 && minLength > textLength))
- {
- minLength = Math.Max(this._mustInput ? 1 : 0, minLength);
- if (text == null)
- {
- text = string.Empty;
- }
- if (this._textPad == TextPadStyle.Left)
- {
- text = text.PadLeft(minLength, this._paddingChar.Value);
- }
- else
- {
- text = text.PadRight(minLength, this._paddingChar.Value);
- }
- }
- }
- return text;
- }
- /// <summary>
- /// 限制文本长度
- /// </summary>
- /// <param name="input"></param>
- /// <param name="ignoreText"></param>
- /// <returns></returns>
- private string LimitLength(string input, bool ignoreText = false)
- {
- if (this.MaxLength == 0 ||
- string.IsNullOrEmpty(input))
- {
- return input;
- }
- int inputCount = input.Length;
- int remainCount = this.MaxLength;
- if (!ignoreText)
- {
- remainCount = remainCount - (base.Text.Length - this.SelectionLength);
- }
- if (remainCount <= 0)
- {
- return string.Empty;
- }
- if (inputCount <= remainCount)
- {
- return input;
- }
- else
- {
- return input.Substring(0, remainCount);
- }
- }
- /// <summary>
- /// 粘贴文本
- /// </summary>
- private void PasteChars()
- {
- if (!this.Enabled || this.ReadOnly)
- {
- return;
- }
- object clipboardChars = Clipboard.GetDataObject().GetData(DataFormats.UnicodeText);
- if (clipboardChars == null)
- {
- clipboardChars = Clipboard.GetDataObject().GetData(DataFormats.Text);
- if (clipboardChars == null)
- {
- return;
- }
- }
- this.PasteChars(clipboardChars.ToString());
- }
- /// <summary>
- /// 粘贴文本
- /// </summary>
- /// <param name="chars"></param>
- private void PasteChars(string chars)
- {
- if (string.IsNullOrEmpty(chars))
- {
- WindowsAPI.MessageBeep(BeepType.MB_OK);
- return;
- }
- chars = this.RejectChars(chars);
- chars = this.LimitLength(chars);
- if (chars.Length > 0)
- {
- TextPasteEventArgs pasteEventArgs = new TextPasteEventArgs(chars);
- this.OnPaste(pasteEventArgs);
- if (pasteEventArgs.Cancel)
- {
- return;
- }
- this._isSettingText = true;
- base.SelectedText = chars;
- this._isSettingText = false;
- }
- else
- {
- WindowsAPI.MessageBeep(BeepType.MB_OK);
- }
- }
- #endregion
- #region IDataVerifiable 成员
- #region 成员变量
- /// <summary>
- /// 显示边框颜色
- /// </summary>
- private bool _showBorderColor = true;
- /// <summary>
- /// 控件的项目名
- /// </summary>
- private string _itemName = null;
- /// <summary>
- /// 控件是否是必须输入项目
- /// </summary>
- private bool _mustInput = false;
- /// <summary>
- /// 控件在验证输入错误时,如何提示
- /// </summary>
- private InputErrorAlert _errorAlert = InputErrorAlert.Validated;
- /// <summary>
- /// 是否显示必须输入项目的提示
- /// </summary>
- private bool _showMustInputAlert = true;
- /// <summary>
- /// 验证不通过时,焦点能否离开
- /// </summary>
- private bool _canLostFocusOnError = true;
- /// <summary>
- /// 验证是否有错误
- /// </summary>
- private bool _hasError = false;
- /// <summary>
- /// 是否自定义错误
- /// </summary>
- private bool _hasCustomerError = false;
- /// <summary>
- /// 错误编码
- /// </summary>
- private ControlErrorCode _errorCode = ControlErrorCode.DKC_0000;
- /// <summary>
- /// 错误消息
- /// </summary>
- private string _errorMessage = null;
- /// <summary>
- /// 自定义错误消息
- /// </summary>
- private string _customerErrorMessage = null;
- #endregion
- #region 属性
- /// <summary>
- /// 获取或设置控件是否显示边框颜色。
- /// </summary>
- [Description("获取或设置控件是否显示边框颜色。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool ShowBorderColor
- {
- get
- {
- return this._showBorderColor;
- }
- set
- {
- if (this._showBorderColor != value)
- {
- this._showBorderColor = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件的项目名
- /// </summary>
- [Description("获取或设置控件的项目名。"), Category("IDataVerifiable")]
- [DefaultValue(null)]
- public string CDItemName
- {
- get
- {
- return this._itemName;
- }
- set
- {
- this._itemName = value;
- }
- }
- /// <summary>
- /// 获取或设置控件是否必须选中项目。
- /// </summary>
- [Description("获取或设置控件是否必须选中项目。"), Category("IDataVerifiable")]
- [DefaultValue(false)]
- public bool MustInput
- {
- get
- {
- return this._mustInput;
- }
- set
- {
- if (this._mustInput != value)
- {
- if (!this._mustInput && value)
- {
- this._mustInput = value;
- this.SetTextValue();
- }
- else
- {
- this._mustInput = value;
- }
- this.ValidateData();
- if (value && this._showMustInputAlert)
- {
- this.InvalidateBorder();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否必须选中项目。
- /// </summary>
- [Description("获取或设置控件在验证输入错误时,如何提示。"), Category("IDataVerifiable")]
- [DefaultValue(typeof(InputErrorAlert), "Validated")]
- public InputErrorAlert InputErrorAlert
- {
- get
- {
- return this._errorAlert;
- }
- set
- {
- if (this._errorAlert != value)
- {
- this._errorAlert = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否显示必须输入项目提示
- /// </summary>
- [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool ShowMustInputAlert
- {
- get
- {
- return this._showMustInputAlert;
- }
- set
- {
- if (this._showMustInputAlert != value)
- {
- this._showMustInputAlert = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置当验证不通过时,控件是否可以失去焦点
- /// </summary>
- [Description("获取或设置当验证不通过时,控件是否可以失去焦点。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool CanLostFocusOnError
- {
- get
- {
- return this._canLostFocusOnError;
- }
- set
- {
- this._canLostFocusOnError = value;
- }
- }
- /// <summary>
- /// 获取控件校验时是否有错误
- /// </summary>
- [Description("获取控件校验时是否有错误。"), Category("IDataVerifiable")]
- [DefaultValue(false)]
- public bool HasError
- {
- get
- {
- return this._hasCustomerError || this._hasError;
- }
- }
- /// <summary>
- /// 获取控件校验时的错误编码
- /// </summary>
- [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
- [DefaultValue(typeof(ControlErrorCode), "DKC_0000")]
- public ControlErrorCode ErrorCode
- {
- get
- {
- return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode;
- }
- }
- /// <summary>
- /// 获取控件校验时的错误消息
- /// </summary>
- [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
- [DefaultValue(null)]
- public string ErrorMessage
- {
- get
- {
- return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage;
- }
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 设置自定义错误
- /// </summary>
- /// <param name="hasError">输入是否有错误</param>
- /// <param name="errorMessage">错误消息</param>
- public virtual void SetCustomerError(bool hasError, string errorMessage)
- {
- if (this._hasCustomerError != hasError ||
- this._customerErrorMessage != errorMessage)
- {
- this._hasCustomerError = hasError;
- this._customerErrorMessage = errorMessage;
- this.OnHasErrorChanged(EventArgs.Empty);
- this.InvalidateBorder();
- }
- }
- /// <summary>
- /// 清除自定义错误
- /// </summary>
- public virtual void ClearCustomerError()
- {
- this.SetCustomerError(false, null);
- }
- /// <summary>
- /// 验证输入内容
- /// </summary>
- /// <returns>验证结果</returns>
- public virtual bool ValidateData()
- {
- string text = base.Text;
- if (this._mustInput && text.Length == 0)
- {
- this.SetError(true, ControlErrorCode.DKC_0001, this._itemName);
- return true;
- }
- if (this._minLength >0 &&
- text.Length > 0 &&
- this._minLength > text.Length)
- {
- this.SetError(true, ControlErrorCode.DKC_0002, this._itemName, this._minLength);
- return true;
- }
- if (text.Length > 0 &&
- this._regular != null &&
- this._regular.Length > 0)
- {
- if (!Regex.IsMatch(text, this._regular))
- {
- this.SetError(true, ControlErrorCode.DKC_0004, this._itemName);
- return true;
- }
- }
- this.ClearError();
- return false;
- }
- /// <summary>
- /// 清除输入项
- /// </summary>
- public virtual void ClearValue()
- {
- this.Clear();
- }
- #endregion
- #region 保护方法
- /// <summary>
- /// 设置验证不通过错误
- /// </summary>
- /// <param name="hasError">是否有错误</param>
- /// <param name="code">错误编码</param>
- /// <param name="args">设置格式的对象</param>
- protected void SetError(bool hasError, ControlErrorCode code, params object[] args)
- {
- if (this._hasError != hasError ||
- this._errorCode != code)
- {
- this._hasError = hasError;
- this._errorCode = code;
- if (args != null && args.Length > 0)
- {
- this._errorMessage = string.Format(code.GetDescription(), args);
- }
- else
- {
- this._errorMessage = code.GetDescription();
- }
- this.OnHasErrorChanged(EventArgs.Empty);
- this.InvalidateBorder();
- }
- }
- /// <summary>
- /// 清除验证不通过错误
- /// </summary>
- protected void ClearError()
- {
- this.SetError(false, ControlErrorCode.DKC_0000);
- }
- #endregion
- #endregion
- #region IAsyncControl 成员
- #region 成员变量
- /// <summary>
- /// 异步处理开始时,控件状态
- /// </summary>
- private bool _asyncBeginStatus = false;
- private bool _asyncBeginFocused = false;
- #endregion
- #region 公有方法
- /// <summary>
- /// 开始异步处理
- /// </summary>
- /// <param name="doFocus">是否处理焦点</param>
- public virtual void BeginAsync(ref bool doFocus)
- {
- this._asyncBeginFocused = false;
- if (doFocus && this.Focused)
- {
- this._asyncBeginFocused = true;
- doFocus = false;
- }
- this._asyncBeginStatus = this.ReadOnly;
- this.ReadOnly = true;
- }
- /// <summary>
- /// 结束异步处理
- /// </summary>
- public virtual void EndAsync()
- {
- this.ReadOnly = this._asyncBeginStatus;
- if (this._asyncBeginFocused)
- {
- this.Focus();
- }
- }
- #endregion
- #endregion
- }
- }
|