using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Security.Permissions; using System.Windows.Forms; using Dongke.WinForm.Utilities; namespace Dongke.WinForm.Controls { /// /// 标准复选框列表控件 /// [ToolboxBitmap(typeof(CheckedListBox))] [DefaultBindingProperty("SelectedValue"), DefaultProperty("Items"), DefaultEvent("ItemChecked")] [LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "SelectedValue")] public class CklCheckedListBox : CheckedListBox, IDKControl, IDataVerifiable, IAsyncControl { #region 事件声明 #region ItemChecked /// /// 当某项的选中状态更改后发生 /// private static readonly object EventItemChecked = new object(); /// /// 当某项的选中状态更改后发生。 /// [Description("当某项的选中状态更改后发生。"), Category("CustomerEx")] public event ItemCheckedEventHandler ItemChecked { add { base.Events.AddHandler(EventItemChecked, value); } remove { base.Events.RemoveHandler(EventItemChecked, value); } } #endregion #region HasErrorChanged /// /// 当 HasError 属性的值更改时发生。 /// private static readonly object EventHasErrorChanged = new object(); /// /// 当 HasError 属性的值更改时发生。 /// [Description("当 HasError 属性的值更改时发生。"), Category("CustomerEx")] public event EventHandler HasErrorChanged { add { base.Events.AddHandler(EventHasErrorChanged, value); } remove { base.Events.RemoveHandler(EventHasErrorChanged, value); } } #endregion #endregion #region 成员变量 /// /// 焦点是否进入控件 /// private bool _entered = false; /// /// 鼠标是否进入控件 /// private bool _mouseOver = false; /// /// 边框颜色 /// private Color? _borderColor = null; /// /// 是否单选 /// private bool _simpleChecked = false; /// /// 正在处理必须选中 /// private bool _isMustChecking = false; /// /// 正在处理单项选中 /// private bool _isSimpleChecking = false; /// /// 上次选中项个数 /// private int _inputCount = -1; #endregion #region 构造函数 /// /// 标准复选框列表控件 /// public CklCheckedListBox() { base.CheckOnClick = true; base.FormattingEnabled = false; base.ThreeDCheckBoxes = false; CommonSetting.InitControls(this); } #endregion #region 属性 /// /// 获取选中的文本 /// [Description("获取选中的文本。"), Category("CustomerEx")] [DefaultValue(null)] public string[] CheckedTexts { get { List checkedTexts = this.GetCheckedItemTexts(); return checkedTexts.ToArray(); } } /// /// 获取选中的值 /// [Description("获取选中的值。"), Category("CustomerEx")] [DefaultValue(null)] public object[] CheckedValues { get { List checkedValues = this.GetCheckedItemValues(); return checkedValues.ToArray(); } } /// /// 获取选中项的文本表示形式,用【,】隔开。 /// [Description("获取选中项的文本表示形式,用【,】隔开。"), Category("CustomerEx")] [DefaultValue(null)] public string CheckedText { get { string text = string.Join(Constant.MULTIPLE_TEXT_SEPARATOR, this.GetCheckedItemTexts()); return text; } } /// /// 获取选中项的值,用【,】隔开 /// [Description("获取选中项的值,用【,】隔开。"), Category("CustomerEx")] [DefaultValue(null)] public string CheckedValue { get { string value = string.Join(Constant.MULTIPLE_VALUE_SEPARATOR, this.GetCheckedItemValues()); return value; } } /// /// 获取或设置控件是否单项选中。 /// [Description("获取或设置控件是否单项选中。"), Category("CustomerEx")] [DefaultValue(false)] public bool SimpleChecked { get { return this._simpleChecked; } set { if (this._simpleChecked != value) { this._simpleChecked = value; this.ResetMustOrSimpleChecked(); } } } /// /// 获取一个值,该值指示控件是否有输入焦点。 /// [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(); } } } /// /// 获取一个值,该值指示鼠标是否在控件上方。 /// [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(); } } } #endregion #region 重写属性 /// /// 获取或设置一个值,该值指示当选定项时是否应切换复选框 /// [DefaultValue(true)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] //[EditorBrowsable(EditorBrowsableState.Never)] public new bool CheckOnClick { get { return base.CheckOnClick; } set { base.CheckOnClick = value; } } /// /// 获取或设置控件的数据源 /// [Description("获取或设置控件的数据源。"), Category("CustomerEx")] [DefaultValue(null)] [Browsable(true)] public new object DataSource { get { return base.DataSource; } set { string displayMember = base.DisplayMember; base.DataSource = value; base.DisplayMember = displayMember; //if (!this._mustInput) { this.SelectedIndex = -1; } } } /// /// 获取或设置一个字符串,该字符串指定要显示其内容的列表框中所含对象的属性 /// [Description("获取或设置一个字符串,该字符串指定要显示其内容的列表框中所含对象的属性。"), Category("CustomerEx")] [DefaultValue("")] [Browsable(true)] public new string DisplayMember { get { return base.DisplayMember; } set { base.DisplayMember = value; } } /// /// 获取或设置一个字符串,该字符串指定要从中取值的数据源的属性 /// [Description("获取或设置一个字符串,该字符串指定要从中取值的数据源的属性。"), Category("CustomerEx")] [DefaultValue("")] [Browsable(true)] public new string ValueMember { get { return base.ValueMember; } set { base.ValueMember = value; } } #endregion #region 重写事件 #region 属性改变 /// /// 引发 DataSourceChanged 事件。 /// /// protected override void OnDataSourceChanged(EventArgs e) { try { base.OnDataSourceChanged(e); } catch { } this.ResetMustOrSimpleChecked(); } /// /// /// /// protected override void OnDisplayMemberChanged(EventArgs e) { base.OnDisplayMemberChanged(e); this.ResetMustOrSimpleChecked(); } /// /// /// /// protected override void OnValueMemberChanged(EventArgs e) { base.OnValueMemberChanged(e); this.ResetMustOrSimpleChecked(); } /// /// 引发 HasErrorChanged 事件 /// /// 包含事件数据的 EventArgs protected virtual void OnHasErrorChanged(EventArgs e) { EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged]; if (eventHandler != null) { eventHandler(this, e); } } #endregion #region 行为事件 /// /// 引发 ItemCheck 事件 /// /// protected override void OnItemCheck(ItemCheckEventArgs ice) { if (this._isMustChecking) { return; } if (this._isSimpleChecking) { base.OnItemCheck(ice); if (ice.NewValue == CheckState.Indeterminate) { ice.NewValue = CheckState.Checked; } return; } int checkedCount = (ice.NewValue == CheckState.Unchecked ? -1 : 1) + this.CheckedIndices.Count; if (this._mustInput && checkedCount == 0) { this._isMustChecking = true; //base.SetItemChecked(ice.Index, true); ice.NewValue = CheckState.Checked; this._isMustChecking = false; return; } if (this._simpleChecked && checkedCount > 1) { this._isSimpleChecking = true; foreach (int index in this.CheckedIndices) { if (index != ice.Index) { base.SetItemChecked(index, false); //this.SetItemChecked(index, false); } } this._isSimpleChecking = false; } base.OnItemCheck(ice); if (ice.NewValue == CheckState.Indeterminate) { ice.NewValue = CheckState.Checked; } } /// /// 引发 ItemChecked 事件 /// /// 包含事件数据的 ItemCheckedEventArgs protected virtual void OnItemChecked(ItemCheckedEventArgs ice) { ItemCheckedEventHandler eventHandler = (ItemCheckedEventHandler)base.Events[EventItemChecked]; if (eventHandler != null) { eventHandler(this, ice); } } #endregion #region 键盘事件 /// /// 引发 System.Windows.Forms.Control.KeyPress 事件 /// /// 所引发的 System.Windows.Forms.KeyPressEventArgs protected override void OnKeyPress(KeyPressEventArgs e) { if (e.KeyChar == Constant.C_SPACE && this.SelectionMode != SelectionMode.None) { int selectedIndex = this.SelectedIndex; if (selectedIndex < 0 || selectedIndex >= this.Items.Count) { base.OnKeyPress(e); return; } CheckState checkedState = this.GetItemCheckState(selectedIndex); base.OnKeyPress(e); CheckState newCheckValue = this.GetItemCheckState(selectedIndex); if (checkedState != newCheckValue) { this.CheckOnItemChecked(); ItemCheckedEventArgs itemCheckedEventArgs = new ItemCheckedEventArgs(selectedIndex, newCheckValue, checkedState); this.OnItemChecked(itemCheckedEventArgs); } } else { base.OnKeyPress(e); } } /// /// 键盘点击 /// /// protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (this.SelectedIndex < 1 && (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left)) { this.SelectPrevItem(); e.Handled = true; return; } if (this.SelectedIndex == this.Items.Count - 1 && (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)) { this.SelectNextItem(); e.Handled = true; return; } } #endregion #region 焦点事件 /// /// 输入焦点进入控件 /// /// protected override void OnEnter(EventArgs e) { this.Entered = true; base.OnEnter(e); } /// /// 获得焦点 /// /// protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); } /// /// 失去焦点 /// /// protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); } /// /// 输入焦点离开控件 /// /// protected override void OnLeave(EventArgs e) { base.OnLeave(e); //this.Entered = false; } /// /// 控件正在验证 /// /// protected override void OnValidating(CancelEventArgs e) { base.OnValidating(e); if (this.HasError && !this._canLostFocusOnError) { e.Cancel = true; } } /// /// 控件完成验证 /// /// protected override void OnValidated(EventArgs e) { this.Entered = false; base.OnValidated(e); } #endregion #region 鼠标事件 /// /// 鼠标进入控件 /// /// protected override void OnMouseEnter(EventArgs e) { this.MouseOver = true; base.OnMouseEnter(e); } /// /// 鼠标离开控件 /// /// protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); this.MouseOver = false; } /// /// 鼠标移动 /// /// protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); } /// /// 鼠标按下 /// /// protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); } /// /// 鼠标抬起 /// /// protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); } #endregion #endregion #region 重写方法 /// /// 处理 System.Windows.Forms.CheckedListBox 控件从顶级窗口中接收的命令消息 /// /// 顶级窗口发送到此控件的 System.Windows.Forms.Message [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] protected override void WmReflectCommand(ref Message m) { int n = (int)((long)m.WParam); n = n >> 16 & 65535; switch (n) { case 1: case 2: int selectedIndex = this.SelectedIndex; if (selectedIndex < 0 || selectedIndex >= this.Items.Count) { base.WmReflectCommand(ref m); return; } CheckState checkedState = this.GetItemCheckState(selectedIndex); base.WmReflectCommand(ref m); CheckState newCheckValue = this.GetItemCheckState(selectedIndex); if (checkedState != newCheckValue) { this.CheckOnItemChecked(); ItemCheckedEventArgs itemCheckEventArgs = new ItemCheckedEventArgs(selectedIndex, newCheckValue, checkedState); this.OnItemChecked(itemCheckEventArgs); } return; default: base.WmReflectCommand(ref m); return; } } /// /// 处理 Windows 消息 /// /// 要处理的 Windows System.Windows.Forms.Message protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == (int)WindowsMessage.WM_PAINT || m.Msg == (int)WindowsMessage.WM_NCPAINT || m.Msg == (int)WindowsMessage.WM_CTLCOLOREDIT) { //this.WmBorderPaint(ref m); BorderColorPaint.WmBorderPaint(this._borderColor, this.Width, this.Height, ref m); } } #endregion #region 公有方法 /// /// 获取指定项的成员属性的值 /// /// 项 /// public virtual object GetItemValue(object item) { if (this.DataManager != null) { string dataMember = this.ValueMember; if (dataMember == null) { dataMember = string.Empty; } int num = dataMember.LastIndexOf(Constant.S_POINT); if (num != -1) { dataMember = dataMember.Substring(num + 1); } return this.FilterItemOnProperty(item, dataMember); } return null; } /// /// 获取选中项的文本表示形式 /// /// 选中项的文本 public virtual List GetCheckedItemTexts() { List checkedTexts = new List(); foreach (object item in this.CheckedItems) { checkedTexts.Add(this.GetItemText(item)); } return checkedTexts; } /// /// 获取选中项的成员属性的值 /// /// 选中项的值 public virtual List GetCheckedItemValues() { return this.GetCheckedItemValues(); } /// /// 获取选中项的成员属性的值 /// /// 属性值的类型 /// 选中项的值 public virtual List GetCheckedItemValues() { List checkedValues = new List(); foreach (object item in this.CheckedItems) { checkedValues.Add((T)this.GetItemValue(item)); } return checkedValues; } /// /// 设定全部项的复选状态 /// /// 若要将该项设置为选中,则为 true;否则为 false public virtual void SetItemsChecked(bool value) { for (int i = 0; i < this.Items.Count; i++) { this.SetItemChecked(value, i); } } /// /// 获取全部项的复选状态 /// /// 选中状态 public virtual CheckState GetItemsCheckState() { int checkedItemsCount = this.CheckedItems.Count; if (checkedItemsCount == 0) { return CheckState.Unchecked; } int itemsCount = this.Items.Count; if (checkedItemsCount != itemsCount) { return CheckState.Indeterminate; } if (!this.ThreeDCheckBoxes) { return CheckState.Checked; } for (int i = 0; i < this.CheckedItems.Count; i++) { CheckState cs = this.GetItemCheckState(i); if (cs == CheckState.Indeterminate) { return CheckState.Indeterminate; } } return CheckState.Checked; } /// /// 设定指定项的复选状态 /// /// 若要将该项设置为选中,则为 true;否则为 false /// 要为其设置复选状态的项 public virtual void SetItemCheckedByItem(bool value, params object[] items) { foreach (object item in items) { for (int i = 0; i < this.Items.Count; i++) { object itemInner = this.Items[i]; if (itemInner == item) { this.SetItemChecked(value, i); return; } } } } /// /// 设定指定值的项的复选状态 /// /// 若要将该项设置为选中,则为 true;否则为 false /// 要为其设置复选状态的项的值 public virtual void SetItemCheckedByValue(bool value, params object[] itemValues) { foreach (object itemValue in itemValues) { for (int i = 0; i < this.Items.Count; i++) { object itemValueInner = this.GetItemValue(this.Items[i]); if ((itemValueInner == null && itemValue == null) || (itemValueInner != null && itemValueInner.Equals(itemValue))) { this.SetItemChecked(value, i); break; } } } } /// /// 设定指定值的项的复选状态 /// /// 若要将该项设置为选中,则为 true;否则为 false /// 要为其设置复选状态的项的值 public virtual void SetItemCheckedByValueID(bool value, params string[] itemValues) { foreach (string itemValue in itemValues) { for (int i = 0; i < this.Items.Count; i++) { object itemValueInner = this.GetItemValue(this.Items[i]); if ((itemValueInner == null && itemValue == null) || (itemValue != null && itemValue == itemValueInner + "")) { this.SetItemChecked(value, i); break; } } } } /// /// 设定指定索引处的项目选中或取消 /// /// 若要将该项设置为选中,则为 true;否则为 false /// 要为其设置复选状态的项的索引 public void SetItemChecked(bool value, params int[] indexs) { foreach (int index in indexs) { //this.SetItemCheckState(index, value ? CheckState.Checked : CheckState.Unchecked); // 响应OnItemChecked事件 this.SetItemChecked(index, value); } } /// /// 设置指定索引处项的复选状态 /// /// 要为其设置状态的项的索引 /// 若要将该项设置为选中,则为 true;否则为 false public new void SetItemChecked(int index, bool value) { bool checkedValue = this.GetItemChecked(index); base.SetItemChecked(index, value); if (value != checkedValue) { this.CheckOnItemChecked(); ItemCheckedEventArgs itemCheckEventArgs = new ItemCheckedEventArgs(index, value ? CheckState.Checked : CheckState.Unchecked, checkedValue ? CheckState.Checked : CheckState.Unchecked); this.OnItemChecked(itemCheckEventArgs); } } /// /// 设置指定索引处项的复选状态 /// /// 要为其设置状态的项的索引 /// System.Windows.Forms.CheckState 值之一 public new void SetItemCheckState(int index, CheckState value) { CheckState checkedState = this.GetItemCheckState(index); base.SetItemCheckState(index, value); if (value != checkedState) { this.CheckOnItemChecked(); ItemCheckedEventArgs itemCheckEventArgs = new ItemCheckedEventArgs(index, value, checkedState); this.OnItemChecked(itemCheckEventArgs); } } /// /// 选择下一个Item /// /// 是否能选择空 public void SelectNextItem(bool clearSelect = true) { int boxItemCount = this.Items.Count; if (boxItemCount < 1) { return; } int boxSelectedIndex = this.SelectedIndex; boxSelectedIndex++; if (boxSelectedIndex >= boxItemCount) { if (this._mustInput || !clearSelect) { boxSelectedIndex = 0; } else { boxSelectedIndex = -1; } } if (boxSelectedIndex < 0) { this.SelectedIndex = 0; this.ClearSelected(); } else { this.SelectedIndex = boxSelectedIndex; } } /// /// 选择上一个Item /// /// 是否能选择空 public void SelectPrevItem(bool clearSelect = true) { int boxItemCount = this.Items.Count; if (boxItemCount < 1) { return; } int boxSelectedIndex = this.SelectedIndex; boxSelectedIndex--; if (boxSelectedIndex < -1 || ((this._mustInput || !clearSelect) && boxSelectedIndex < 0)) { boxSelectedIndex = boxItemCount - 1; } if (boxSelectedIndex < 0) { this.ClearSelected(); } else { this.SelectedIndex = boxSelectedIndex; } } #endregion #region 保护方法 /// /// 重置全部项的复选状态(必选或单选) /// internal protected virtual void ResetMustOrSimpleChecked() { if (this._mustInput && this.CheckedIndices.Count == 0 && this.Items.Count > 0) { int index = this.SelectedIndex < 0 ? 0 : this.SelectedIndex; this.SetItemChecked(index, true); return; } if (this._simpleChecked && this.CheckedIndices.Count > 1) { this._isSimpleChecking = true; try { int index = this.CheckedIndices.Contains(this.SelectedIndex) ? this.SelectedIndex : this.CheckedIndices[0]; foreach (int checkedIndex in this.CheckedIndices) { if (checkedIndex != index) { this.SetItemChecked(checkedIndex, false); } } } finally { this._isSimpleChecking = false; } return; } } #endregion #region 私有方法 /// /// 当ItemChecked事件发生时,验证Error。 /// private void CheckOnItemChecked() { if (this._mustInput) { if (this._inputCount != this.CheckedItems.Count) { this._inputCount = this.CheckedItems.Count; if (this._inputCount > 0) { this.ClearError(); } else { if (this.SelectedIndex > 0) { this.SetItemChecked(this.SelectedIndex, true); return; } if (this.Items.Count > 0) { this.SetItemChecked(0, true); return; } this.SetError(true, ControlErrorCode.DKC_0001, this._itemName); } } } } /// /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。 /// 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); } } } #endregion #region IDataVerifiable 成员 #region 成员变量 /// /// 显示边框颜色 /// private bool _showBorderColor = true; /// /// 控件的项目名 /// private string _itemName = null; /// /// 控件是否是必须输入项目 /// private bool _mustInput = false; /// /// 控件在验证输入错误时,如何提示 /// private InputErrorAlert _errorAlert = InputErrorAlert.Validated; /// /// 是否显示必须输入项目的提示 /// private bool _showMustInputAlert = true; /// /// 验证不通过时,焦点能否离开 /// private bool _canLostFocusOnError = true; /// /// 验证是否有错误 /// private bool _hasError = false; /// /// 是否自定义错误 /// private bool _hasCustomerError = false; /// /// 错误编码 /// private ControlErrorCode _errorCode = ControlErrorCode.DKC_0000; /// /// 错误消息 /// private string _errorMessage = null; /// /// 自定义错误消息 /// private string _customerErrorMessage = null; #endregion #region 属性 /// /// 获取或设置控件是否显示边框颜色。 /// [Description("获取或设置控件是否显示边框颜色。"), Category("IDataVerifiable")] [DefaultValue(true)] public bool ShowBorderColor { get { return this._showBorderColor; } set { if (this._showBorderColor != value) { this._showBorderColor = value; this.InvalidateBorder(); } } } /// /// 获取或设置控件的项目名 /// [Description("获取或设置控件的项目名。"), Category("IDataVerifiable")] [DefaultValue(null)] public string CDItemName { get { return this._itemName; } set { this._itemName = value; } } /// /// 获取或设置控件是否必须选中项目。 /// [Description("获取或设置控件是否必须选中项目。"), Category("IDataVerifiable")] [DefaultValue(false)] public bool MustInput { get { return this._mustInput; } set { if (this._mustInput != value) { this._mustInput = value; this.ResetMustOrSimpleChecked(); this.ValidateData(); if (this._mustInput && this._showMustInputAlert) { this.InvalidateBorder(); } } } } /// /// 获取或设置控件是否必须选中项目。 /// [Description("获取或设置控件在验证输入错误时,如何提示。"), Category("IDataVerifiable")] [DefaultValue(typeof(InputErrorAlert), "Validated")] public InputErrorAlert InputErrorAlert { get { return this._errorAlert; } set { if (this._errorAlert != value) { this._errorAlert = value; this.InvalidateBorder(); } } } /// /// 获取或设置控件是否显示必须输入项目提示 /// [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IDataVerifiable")] [DefaultValue(true)] public bool ShowMustInputAlert { get { return this._showMustInputAlert; } set { if (this._showMustInputAlert != value) { this._showMustInputAlert = value; this.InvalidateBorder(); } } } /// /// 获取或设置当验证不通过时,控件是否可以失去焦点 /// [Description("获取或设置当验证不通过时,控件是否可以失去焦点。"), Category("IDataVerifiable")] [DefaultValue(true)] public bool CanLostFocusOnError { get { return this._canLostFocusOnError; } set { this._canLostFocusOnError = value; } } /// /// 获取控件校验时是否有错误 /// [Description("获取控件校验时是否有错误。"), Category("IDataVerifiable")] [DefaultValue(false)] public bool HasError { get { return this._hasCustomerError || this._hasError; } } /// /// 获取控件校验时的错误编码 /// [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")] [DefaultValue(typeof(ControlErrorCode), "DKC_0000")] public ControlErrorCode ErrorCode { get { return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode; } } /// /// 获取控件校验时的错误消息 /// [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")] [DefaultValue(null)] public string ErrorMessage { get { return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage; } } #endregion #region 公有方法 /// /// 设置自定义错误 /// /// 输入是否有错误 /// 错误消息 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(); } } /// /// 清除自定义错误 /// public virtual void ClearCustomerError() { this.SetCustomerError(false, null); } /// /// 验证输入内容 /// /// 验证结果 public virtual bool ValidateData() { if (this._mustInput && this.CheckedItems.Count == 0) { this.SetError(true, ControlErrorCode.DKC_0001, this._itemName); return false; } this.ClearError(); return true; } /// /// 清除输入项 /// public virtual void ClearValue() { this.SetItemsChecked(false); } #endregion #region 保护方法 /// /// 设置验证不通过错误 /// /// 是否有错误 /// 错误编码 /// 设置格式的对象 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(); } } /// /// 清除验证不通过错误 /// protected void ClearError() { this.SetError(false, ControlErrorCode.DKC_0000); } #endregion #endregion #region IAsyncControl 成员 #region 成员变量 /// /// 异步处理开始时,控件状态 /// private bool _asyncBeginStatus = false; private bool _asyncBeginFocused = false; #endregion #region 公有方法 /// /// 开始异步处理 /// /// 是否处理焦点 public virtual void BeginAsync(ref bool doFocus) { this._asyncBeginFocused = false; if (doFocus && this.Focused) { this._asyncBeginFocused = true; doFocus = false; } this._asyncBeginStatus = this.Enabled; this.Enabled = false; } /// /// 结束异步处理 /// public virtual void EndAsync() { this.Enabled = this._asyncBeginStatus; if (this._asyncBeginFocused) { this.Focus(); } } #endregion #endregion } }