using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Runtime; 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")] //[Designer("System.Windows.Forms.Design.ListBoxDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] public partial class CalCheckAllListBox : UserControl, IDKControl, IPopupControl, IDataVerifiable, IAsyncControl { #region 事件声明 #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? _isCheckAll = null; /// /// 不确定选中时,选中的Item的Index /// private List _indeterminateIndices = new List(); /// /// 焦点是否进入控件 /// private bool _entered = false; /// /// 鼠标是否进入控件 /// private bool _mouseOver = false; /// /// 边框颜色 /// private Color? _borderColor = null; //private bool _isAllChecked = false; #endregion #region 构造函数 /// /// 扩展的复选框列表控件:可以全部选择 /// public CalCheckAllListBox() { this.InitializeComponent(); base.BackColor = SystemColors.Window; base.BorderStyle = BorderStyle.FixedSingle; this.chkAll.MouseWheel += chkAll_MouseWheel; this.chkAll.GotFocus += chkAll_GotFocus; } #endregion #region 实现 #region ListControl #region 属性 /// /// 获取或设置控件的数据源 /// [Description("获取或设置控件的数据源。"), Category("ListControl")] [AttributeProvider(typeof(IListSource))] [DefaultValue(null)] [RefreshProperties(RefreshProperties.Repaint)] public object DataSource { get { return this.cklItem.DataSource; } set { this.cklItem.DataSource = value; } } /// /// 获取或设置控件的显示的属性 /// [Description("获取或设置控件的显示的属性。"), Category("ListControl")] //[DefaultValue(null)] [DefaultValue("")] [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] public string DisplayMember { get { return this.cklItem.DisplayMember; } set { this.cklItem.DisplayMember = value; } } /// /// 获取或设置提供自定义格式设置行为的 System.IFormatProvider。 /// [Description("获取或设置提供自定义格式设置行为的 System.IFormatProvider。"), Category("ListControl")] [Browsable(false)] [DefaultValue(null)] [EditorBrowsable(EditorBrowsableState.Advanced)] public IFormatProvider FormatInfo { get { return this.cklItem.FormatInfo; } set { this.cklItem.FormatInfo = value; } } /// /// 获取或设置格式说明符字符,指示如何显示值。 /// [Description("获取或设置格式说明符字符,指示如何显示值。"), Category("ListControl")] [DefaultValue("")] [Editor("System.Windows.Forms.Design.FormatStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [MergableProperty(false)] public string FormatString { get { return this.cklItem.FormatString; } set { this.cklItem.FormatString = value; } } /// /// 获取或设置一个值,该值指示是否将格式设置应用于控件的 DisplayMember 属性。 /// [Description("获取或设置一个值,该值指示是否将格式设置应用于控件的 DisplayMember 属性。"), Category("ListControl")] [DefaultValue(false)] public bool FormattingEnabled { get { return this.cklItem.FormattingEnabled; } set { this.cklItem.FormattingEnabled = value; } } /// /// 获取或设置由 DisplayMember 属性指定的成员属性的值。 /// [Description("获取或设置由 DisplayMember 属性指定的成员属性的值。"), Category("ListControl")] [Bindable(true)] [Browsable(false)] [DefaultValue(null)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public object SelectedValue { get { return this.cklItem.SelectedValue; } set { this.cklItem.SelectedValue = value; } } /// /// 获取或设置一个属性,该属性将用作控件中的项的实际值。 /// [Description("获取或设置一个属性,该属性将用作控件中的项的实际值。"), Category("ListControl")] //[DefaultValue(null)] [DefaultValue("")] [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] public string ValueMember { get { return this.cklItem.ValueMember; } set { this.cklItem.ValueMember = value; } } #endregion #region 事件声明 /// /// 当 DataSource 更改时发。 /// [Description("当 DataSource 更改时发。"), Category("ListControl")] public event EventHandler DataSourceChanged { add { this.cklItem.DataSourceChanged += value; } remove { this.cklItem.DataSourceChanged -= value; } } /// /// 当 DisplayMember 属性更改时发生。 /// [Description("当 DisplayMember 属性更改时发生。"), Category("ListControl")] public event EventHandler DisplayMemberChanged { add { this.cklItem.DisplayMemberChanged += value; } remove { this.cklItem.DisplayMemberChanged -= value; } } /// /// 在该控件绑定到数据值时发生。 /// [Description("在该控件绑定到数据值时发生。"), Category("ListControl")] public event ListControlConvertEventHandler Format { add { this.cklItem.Format += value; } remove { this.cklItem.Format -= value; } } /// /// 当 FormatInfo 属性的值更改时发生。 /// [Description("当 FormatInfo 属性的值更改时发生。"), Category("ListControl")] [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Advanced)] public event EventHandler FormatInfoChanged { add { this.cklItem.FormatInfoChanged += value; } remove { this.cklItem.FormatInfoChanged -= value; } } /// /// 当 FormatString 属性的值更改时发生。 /// [Description("当 FormatString 属性的值更改时发生。"), Category("ListControl")] public event EventHandler FormatStringChanged { add { this.cklItem.FormatStringChanged += value; } remove { this.cklItem.FormatStringChanged -= value; } } /// /// 当 FormattingEnabled 属性的值更改时发生。 /// [Description("当 FormattingEnabled 属性的值更改时发生。"), Category("ListControl")] public event EventHandler FormattingEnabledChanged { add { this.cklItem.FormattingEnabledChanged += value; } remove { this.cklItem.FormattingEnabledChanged -= value; } } /// /// 当 SelectedValue 属性更改时发生。 /// [Description("当 SelectedValue 属性更改时发生。"), Category("ListControl")] public event EventHandler SelectedValueChanged { add { this.cklItem.SelectedValueChanged += value; } remove { this.cklItem.SelectedValueChanged -= value; } } /// /// 当 ValueMember 属性更改时发生。 /// [Description("当 ValueMember 属性更改时发生。"), Category("ListControl")] public event EventHandler ValueMemberChanged { add { this.cklItem.ValueMemberChanged += value; } remove { this.cklItem.ValueMemberChanged -= value; } } #endregion #region 公共方法 /// /// 返回指定项的文本表示形式。 /// /// 从中获取要显示的内容的对象 /// /// 如果未指定 DisplayMember 属性,则由 GetItemText(System.Object) /// 返回的值是此项的 ToString 方法的值。否则,该方法返回在 DisplayMember /// 属性中指定的成员的字符串值,该成员被指定给在 item 参数中指定的对象。 /// public string GetItemText(object item) { return this.cklItem.GetItemText(item); } #endregion #endregion #region ListBox #region 属性 /// /// 获取或设置多列显示中列的宽度。 /// [Description("获取或设置多列显示中列的宽度。"), Category("ListBox")] [DefaultValue(0)] [Localizable(true)] public int ColumnWidth { get { return this.cklItem.ColumnWidth; } set { this.cklItem.ColumnWidth = value; } } /// /// 获取控件中的项之间的制表符的宽度。 /// [Description("获取控件中的项之间的制表符的宽度。"), Category("ListBox")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public ListBox.IntegerCollection CustomTabOffsets { get { return this.cklItem.CustomTabOffsets; } } /// /// 获取或设置控件的水平滚动条可滚动的宽度。 /// [Description("获取或设置控件的水平滚动条可滚动的宽度。"), Category("ListBox")] [DefaultValue(0)] [Localizable(true)] public int HorizontalExtent { get { return this.cklItem.HorizontalExtent; } set { this.cklItem.HorizontalExtent = value; } } /// /// 获取或设置一个值,该值指示是否在控件中显示水平滚动条。 /// [Description("获取或设置一个值,该值指示是否在控件中显示水平滚动条。"), Category("ListBox")] [DefaultValue(false)] [Localizable(true)] public bool HorizontalScrollbar { get { return this.cklItem.HorizontalScrollbar; } set { this.cklItem.HorizontalScrollbar = value; } } /// /// 获取或设置一个值,该值指示控件是否应调整大小以避免只显示项的局部。 /// [Description("获取或设置一个值,该值指示控件是否应调整大小以避免只显示项的局部。"), Category("ListBox")] [DefaultValue(true)] [Localizable(true)] [RefreshProperties(RefreshProperties.Repaint)] public bool IntegralHeight { get { return this.cklItem.IntegralHeight; } set { this.cklItem.IntegralHeight = value; } } /// /// 获取或设置一个值,该值指示控件是否支持多列。 /// [Description("获取或设置一个值,该值指示控件是否支持多列。"), Category("ListBox")] [DefaultValue(false)] public bool MultiColumn { get { return this.cklItem.MultiColumn; } set { this.cklItem.MultiColumn = value; } } /// /// 获取控件中所有项的组合高度。 /// [Description("获取控件中所有项的组合高度。"), Category("ListBox")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public int PreferredHeight { get { return this.cklItem.PreferredHeight; } } /// /// 获取或设置一个值,该值指示是否任何时候都显示垂直滚动条。 /// [Description("获取或设置一个值,该值指示是否任何时候都显示垂直滚动条。"), Category("ListBox")] [DefaultValue(false)] [Localizable(true)] public bool ScrollAlwaysVisible { get { return this.cklItem.ScrollAlwaysVisible; } set { this.cklItem.ScrollAlwaysVisible = value; } } /// /// 获取或设置控件中当前选定项的从零开始的索引。 /// [Description("获取或设置控件中当前选定项的从零开始的索引。"), Category("ListBox")] [Bindable(true)] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public virtual int SelectedIndex { get { return this.cklItem.SelectedIndex; } set { this.cklItem.SelectedIndex = value; } } /// /// 获取一个集合,该集合包含控件中所有当前选定项的从零开始的索引。 /// [Description("获取一个集合,该集合包含控件中所有当前选定项的从零开始的索引。"), Category("ListBox")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public ListBox.SelectedIndexCollection SelectedIndices { get { return this.cklItem.SelectedIndices; } } /// /// 获取或设置控件中的当前选定项。 /// [Description("获取或设置控件中的当前选定项。"), Category("ListBox")] [Bindable(true)] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public object SelectedItem { get { return this.cklItem.SelectedItem; } set { this.cklItem.SelectedItem = value; } } /// /// 获取包含控件中当前选定项的集合。 /// [Description("获取包含控件中当前选定项的集合。"), Category("ListBox")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public ListBox.SelectedObjectCollection SelectedItems { get { return this.cklItem.SelectedItems; } } /// /// 获取或设置在控件中选择项所用的方法。 /// [Description("获取或设置在控件中选择项所用的方法。"), Category("ListBox")] [DefaultValue(typeof(SelectionMode), "One")] public virtual SelectionMode SelectionMode { get { return this.cklItem.SelectionMode; } set { this.cklItem.SelectionMode = value; } } /// /// 获取或设置一个值,该值指示控件中的项是否按字母顺序排序。 /// [Description("获取或设置一个值,该值指示控件中的项是否按字母顺序排序。"), Category("ListBox")] [DefaultValue(false)] public bool Sorted { get { return this.cklItem.Sorted; } set { this.cklItem.Sorted = value; } } /// /// 获取或搜索控件中当前选定项的文本。 /// [Description("获取或搜索控件中当前选定项的文本。"), Category("ListBox")] [Bindable(false)] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public virtual string ItemText { get { return this.cklItem.Text; } set { this.cklItem.Text = value; } } /// /// 获取或设置控件中第一个可见项的索引。 /// [Description("获取或设置控件中第一个可见项的索引。"), Category("ListBox")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public int TopIndex { get { return this.cklItem.TopIndex; } set { this.cklItem.TopIndex = value; } } /// /// 获取或设置一个值,它指示控件在通过使用 CustomTabOffsets 整数数组绘制字符串时是否识别并展开制表符。 /// [Description("获取或设置一个值,它指示控件在通过使用 CustomTabOffsets 整数数组绘制字符串时是否识别并展开制表符。"), Category("ListBox")] [Browsable(false)] [DefaultValue(false)] public bool UseCustomTabOffsets { get { return this.cklItem.UseCustomTabOffsets; } set { this.cklItem.UseCustomTabOffsets = value; } } /// /// 获取或设置一个值,该值指示控件在绘制其字符串时是否可识别和展开制表符。 /// [Description("获取或设置一个值,该值指示控件在绘制其字符串时是否可识别和展开制表符。"), Category("ListBox")] [DefaultValue(true)] public bool UseTabStops { get { return this.cklItem.UseTabStops; } set { this.cklItem.UseTabStops = value; } } #region Never /* [Description(""), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public Padding ItemPadding { get { return base.Padding; } set { } } */ /* /// /// 获取或设置控件的绘图模式。 /// [Description("获取或设置控件的绘图模式。"), Category("CustomerEx")] //[Browsable(false)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] //[EditorBrowsable(EditorBrowsableState.Never)] [RefreshProperties(RefreshProperties.Repaint)] [DefaultValue(typeof(DrawMode), "Normal")] public virtual DrawMode DrawMode { get { return this.clstItem.DrawMode; } set { this.clstItem.DrawMode = value; } } /// /// 获取或设置控件中项的高度。 /// [Description("获取或设置控件中项的高度。"), Category("CustomerEx")] //[Browsable(false)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] //[EditorBrowsable(EditorBrowsableState.Never)] //[DefaultValue(13)] [DefaultValue(16)] [Localizable(true)] [RefreshProperties(RefreshProperties.Repaint)] public virtual int ItemHeight { get { return this.clstItem.ItemHeight; } set { this.clstItem.ItemHeight = value; //this.pnlItem.Height = this.clstItem.ItemHeight + 2; } } /// /// 获取控件的项 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] [Description("获取控件的项。"), Category("CustomerEx")] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [Localizable(true)] [MergableProperty(false)] //public ListBox.ObjectCollection Items public ListBox.ObjectCollection ListBoxItems { get { return this.clstItem.Items; } } */ #endregion #endregion #region 事件声明 /// /// 在单击 Item 控件时发生。 /// [Description("在单击 Item 控件时发生。"), Category("ListBox")] [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] public event EventHandler ItemClick { add { this.cklItem.Click += value; } remove { this.cklItem.Click -= value; } } /// /// 在所有者绘制的 Item 在视觉外观更改时发生。 /// [Description("在所有者绘制的 Item 在视觉外观更改时发生。"), Category("ListBox")] public event DrawItemEventHandler DrawItem { add { this.cklItem.DrawItem += value; } remove { this.cklItem.DrawItem -= value; } } /// /// 当用户使用鼠标指针单击 Item 控件时发生。 /// [Description("当用户使用鼠标指针单击 Item 控件时发生。"), Category("ListBox")] [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] public event MouseEventHandler ItemMouseClick { add { this.cklItem.MouseClick += value; } remove { this.cklItem.MouseClick -= value; } } /// /// 在 SelectedIndex 属性或 SelectedIndices集合更改后发生。 /// [Description("在 SelectedIndex 属性或 SelectedIndices集合更改后发生。"), Category("ListBox")] public event EventHandler SelectedIndexChanged { add { this.cklItem.SelectedIndexChanged += value; } remove { this.cklItem.SelectedIndexChanged -= value; } } /// /// 当 Text 属性更改时发生。 /// [Description("当 Text 属性更改时发生。"), Category("ListBox")] [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Advanced)] public event EventHandler ItemTextChanged { add { this.cklItem.TextChanged += value; } remove { this.cklItem.TextChanged -= value; } } #region Never /* /// /// 当 Padding 属性的值更改时发生。 /// [Description("当 Padding 属性的值更改时发生。"), Category("CustomerEx")] [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] public event EventHandler ItemPaddingChanged { add { this.clstItem.PaddingChanged += value; } remove { this.clstItem.PaddingChanged -= value; } } /// /// 在绘制 Item 控件时发生。 /// [Description("在绘制 Item 控件时发生。"), Category("CustomerEx")] [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] public event PaintEventHandler ItemPaint { add { this.clstItem.Paint += value; } remove { this.clstItem.Paint -= value; } } */ /* /// /// 创建所有者描述的控件和确定列表项的大小时发生。 /// //[Browsable(false)] //[EditorBrowsable(EditorBrowsableState.Never)] [Description("创建所有者描述的控件和确定列表项的大小时发生。"), Category("CustomerEx")] public event MeasureItemEventHandler MeasureItem { add { this.clstItem.MeasureItem += value; } remove { this.clstItem.MeasureItem -= value; } } */ #endregion #endregion #region 公共方法 /// /// 当向控件中一次添加一个项时,通过防止该控件绘图来维护性能,直到调用 EndUpdate() 方法为止。 /// public void BeginUpdate() { this.cklItem.BeginUpdate(); } /// /// 取消选择控件中的所有项。 /// public void ClearSelected() { this.cklItem.ClearSelected(); } /// /// 在 BeginUpdate() 方法挂起绘制后,该方法恢复绘制控件。 /// public void EndUpdate() { this.cklItem.EndUpdate(); } /// /// 查找 Item 中以指定字符串开始的第一个项。 /// /// 要搜索的文本 /// 找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public int FindString(string s) { return this.cklItem.FindString(s); } /// /// 查找 Item 中以指定字符串开头的第一个项。搜索从特定的起始索引处开始。 /// /// 要搜索的文本 /// 项的从零开始的索引,该项在要搜索的第一个项之前。设置为负一 (-1) 以从控件的开始处进行搜索。 /// 找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。 public int FindString(string s, int startIndex) { return this.cklItem.FindString(s, startIndex); } /// /// 查找 Item 中第一个精确匹配指定字符串的项。 /// /// 要搜索的文本 /// 找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public int FindStringExact(string s) { return this.cklItem.FindStringExact(s); } /// /// 查找 Item 中第一个精确匹配指定字符串的项。搜索从特定的起始索引处开始。 /// /// 要搜索的文本 /// 项的从零开始的索引,该项在要搜索的第一个项之前。设置为负一 (-1) 以从控件的开始处进行搜索。 /// 找到的第一个项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。 public int FindStringExact(string s, int startIndex) { return this.cklItem.FindStringExact(s, startIndex); } /// /// 返回 Item 中某项的高度。 /// /// 要返回其高度的项的从零开始的索引 /// 指定的项的高度,以像素为单位 public int GetItemHeight(int index) { return this.cklItem.GetItemHeight(index); } /// /// 返回控件中的项的边框。 /// /// 要返回其边框的项的从零开始的索引 /// 表示指定项的边框 public Rectangle GetItemRectangle(int index) { return this.cklItem.GetItemRectangle(index); } /// /// 返回一个值,该值指示是否选定了指定的项 /// /// 项的从零开始的索引,根据该索引确定该项是否被选定 /// 如果当前在控件中选定了指定的项,则为 true;否则为 false。 public bool GetSelected(int index) { return this.cklItem.GetSelected(index); } /// /// 返回指定坐标处的项的从零开始的索引 /// /// System.Drawing.Point 对象,包含用于获取项索引的坐标 /// 在指定坐标处找到的项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。 public int IndexFromPoint(Point p) { return this.cklItem.IndexFromPoint(p); } /// /// 返回指定坐标处的项的从零开始的索引。 /// /// 要搜索的位置的 x 坐标 /// 要搜索的位置的 y 坐标 /// 在指定坐标处找到的项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。 public int IndexFromPoint(int x, int y) { return this.cklItem.IndexFromPoint(x, y); } /// /// 强制控件使其工作区无效并立即重绘自己和任何子控件。 /// public virtual void ItemRefresh() { this.cklItem.Refresh(); } /// /// /// public virtual void ItemResetBackColor() { this.cklItem.ResetBackColor(); } /// /// /// public virtual void ItemResetForeColor() { this.cklItem.ResetForeColor(); } /// /// 选择或清除对 Item 中指定项的选定。 /// /// Item中要选择或清除对其选定的项的从零开始的索引 /// 如果为 true,则选择指定的项;否则为 false public void SetSelected(int index, bool value) { this.cklItem.SetSelected(index, value); } /// /// 返回控件的字符串表示形式。 /// /// 一个字符串,它描述控件类型、控件中的项计数,如果该计数不为 0,则还描述控件中第一项的 Text 属性 public new string ToString() { return this.cklItem.ToString(); } #endregion #endregion #region CheckedListBox #region 属性 /// /// 该控件中选中索引的集合。 /// [Description("该控件中选中索引的集合。"), Category("CheckedListBox")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public CheckedListBox.CheckedIndexCollection CheckedIndices { get { return this.cklItem.CheckedIndices; } } /// /// 该控件中选中项的集合。 /// [Description("该控件中选中项的集合。"), Category("CheckedListBox")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public CheckedListBox.CheckedItemCollection CheckedItems { get { return this.cklItem.CheckedItems; } } /// /// 获取或设置一个值,该值指示当选定项时是否应切换复选框。 /// [Description("获取或设置一个值,该值指示当选定项时是否应切换复选框。"), Category("CheckedListBox")] [DefaultValue(true)] public bool CheckOnClick { get { return this.cklItem.CheckOnClick; } set { this.cklItem.CheckOnClick = value; } } /// /// 获取该控件中项的集合。 /// [Description("获取该控件中项的集合。"), Category("CheckedListBox")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [Localizable(true)] [MergableProperty(false)] public CheckedListBox.ObjectCollection Items { get { return this.cklItem.Items; } } /// /// 获取或设置一个值,该值指示复选框是否有 Flat 或 Normal 的 System.Windows.Forms.ButtonState。 /// [Description("获取或设置一个值,该值指示复选框是否有 Flat 或 Normal 的 System.Windows.Forms.ButtonState。"), Category("CheckedListBox")] [DefaultValue(false)] public bool ThreeDCheckBoxes { get { return this.cklItem.ThreeDCheckBoxes; } set { this.cklItem.ThreeDCheckBoxes = value; } } /// /// 获取或设置一个值,该值确定是使用 System.Drawing.Graphics 类 (GDI+) 还是 System.Windows.Forms.TextRenderer 类 (GDI) 呈现文本。 /// [Description("获取或设置一个值,该值确定是使用 System.Drawing.Graphics 类 (GDI+) 还是 System.Windows.Forms.TextRenderer 类 (GDI) 呈现文本。"), Category("CheckedListBox")] [DefaultValue(false)] public bool UseCompatibleTextRendering { get { return this.cklItem.UseCompatibleTextRendering; } set { this.cklItem.UseCompatibleTextRendering = value; this.chkAll.UseCompatibleTextRendering = value; } } /* // // 摘要: // 获取一个值,该值表示 System.Windows.Forms.CheckedListBox 的绘制元素的模式。此属性与该类无关。 // // 返回结果: // 通常为 Normal 的 System.Windows.Forms.DrawMode。 //[Browsable(false)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] //[EditorBrowsable(EditorBrowsableState.Never)] //public override DrawMode DrawMode //{ // get; // set; //} // // 摘要: // 获取项区域的高度。 // // 返回结果: // 项区域的高度(以像素为单位)。 //[Browsable(false)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] //[EditorBrowsable(EditorBrowsableState.Never)] //public override int ItemHeight //{ // get; // set; //} */ #endregion #region 事件声明 /// /// 当某项的选中状态更改时发生。 /// [Description("当某项的选中状态更改时发生。"), Category("CheckedListBox")] public event ItemCheckEventHandler ItemCheck { add { this.cklItem.ItemCheck += value; } remove { this.cklItem.ItemCheck -= value; } } /* ///// ///// 当用户单击 Item 控件时发生。 ///// //[Description("当用户单击 Item 控件时发生。"), Category("CustomerEx")] //[Browsable(true)] //[EditorBrowsable(EditorBrowsableState.Always)] //public event EventHandler ItemClick //{ // add // { // this.clstItem.Click += value; // } // remove // { // this.clstItem.Click -= value; // } //} ///// ///// 当用户使用鼠标单击 Item 控件时发生。 ///// //[Description("当用户使用鼠标单击 Item 控件时发生。"), Category("CustomerEx")] //[Browsable(true)] //[EditorBrowsable(EditorBrowsableState.Always)] //public event MouseEventHandler ItemMouseClick //{ // add // { // this.clstItem.MouseClick += value; // } // remove // { // this.clstItem.MouseClick -= value; // } //} */ /* // // 摘要: // 在所有者描述的 System.Windows.Forms.CheckedListBox 的可视方位更改时发生。此事件与此类无关。 //[Browsable(false)] //[EditorBrowsable(EditorBrowsableState.Never)] //public event DrawItemEventHandler DrawItem; // // 摘要: // 创建所有者描述的 System.Windows.Forms.ListBox 和确定列表项的大小时发生。此事件与此类无关。 //[Browsable(false)] //[EditorBrowsable(EditorBrowsableState.Never)] //public event MeasureItemEventHandler MeasureItem; */ #endregion #region 公共方法 /// /// 返回指示指定项是否选中的值。 /// /// 项的索引 /// 如果选中该项,则为 true;否则为 false。 public bool GetItemChecked(int index) { return this.cklItem.GetItemChecked(index); } /// /// 返回指示当前项的复选状态的值。 /// /// 要获取其选中值的项的索引 /// System.Windows.Forms.CheckState 值之一。 public CheckState GetItemCheckState(int index) { return this.cklItem.GetItemCheckState(index); } /// /// 将指定索引处的项的 System.Windows.Forms.CheckState 设置为 Checked。 /// /// 要为其设置复选状态的项的索引 /// 若要将该项设置为选中,则为 true;否则为 false public void SetItemChecked(int index, bool value) { this.cklItem.SetItemChecked(index, value); } /// /// 设置指定索引处项的复选状态。 /// /// 要为其设置状态的项的索引 /// System.Windows.Forms.CheckState 值之一 public void SetItemCheckState(int index, CheckState value) { this.cklItem.SetItemCheckState(index, value); } #endregion #endregion #region CklCheckedListBox #region 属性 /// /// 获取选中的文本 /// [Description("获取选中的文本。"), Category("CklCheckedListBox")] [DefaultValue(null)] public string[] CheckedTexts { get { return this.cklItem.CheckedTexts; } } /// /// 获取选中的值 /// [Description("获取选中的值。"), Category("CklCheckedListBox")] [DefaultValue(null)] public object[] CheckedValues { get { return this.cklItem.CheckedValues; } } /// /// 获取选中项的文本表示形式,用【,】隔开。 /// [Description("获取选中项的文本表示形式,用【,】隔开。"), Category("CklCheckedListBox")] [DefaultValue(null)] public string CheckedText { get { return this.cklItem.CheckedText; } } /// /// 获取选中项的值,用【,】隔开 /// [Description("获取选中项的值,用【,】隔开。"), Category("CklCheckedListBox")] [DefaultValue(null)] public string CheckedValue { get { return this.cklItem.CheckedValue; } } ///// ///// 获取或设置控件是否单项选中。 ///// //[Description("获取或设置控件是否单项选中。"), Category("CustomerEx")] //[DefaultValue(false)] ////[Browsable(false)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] //[EditorBrowsable(EditorBrowsableState.Never)] //public bool SimpleChecked //{ // get // { // return this.cklItem.SimpleChecked; // } // set // { // this.cklItem.SimpleChecked = value; // } //} /// /// 获取一个值,该值指示控件是否有输入焦点。 /// [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 事件处理 /// /// 当某项的选中状态更改后发生。 /// [Description("当某项的选中状态更改后发生。"), Category("CklCheckedListBox")] public event ItemCheckedEventHandler ItemChecked { add { this.cklItem.ItemChecked += value; } remove { this.cklItem.ItemChecked -= value; } } #endregion #region 公有方法 /// /// 返回指定项的成员属性的值 /// /// 项 /// public virtual object GetItemValue(object item) { return this.cklItem.GetItemValue(item); } /// /// 返回选中项的文本表示形式 /// /// 选中项的文本 public virtual List GetCheckedItemTexts() { return this.cklItem.GetCheckedItemTexts(); } /// /// 返回选中项的成员属性的值 /// /// 选中项的值 public virtual List GetCheckedItemValues() { return this.cklItem.GetCheckedItemValues(); } /// /// 返回选中项的成员属性的值 /// /// 选中项的值 public virtual List GetCheckedItemValues() { return this.cklItem.GetCheckedItemValues(); } /// /// 设置全部项目选中状态 /// /// 若要将该项设置为选中,则为 true;否则为 false public virtual void SetAllItemsChecked(bool value) { //this.chkAll.Checked = value; if (value) { this.chkAll.CheckState = CheckState.Checked; } else { this.chkAll.CheckState = CheckState.Unchecked; } } /// /// 设置全部项目选中状态 /// /// 若要将该项设置为选中,则为 true;否则为 false public virtual void SetAllItemsChecked(CheckState value) { this.chkAll.CheckState = value; } /// /// 获取全部项目的选中状态 /// /// 选中状态 public virtual CheckState GetItemsCheckState() { //return this.cklItem.GetItemsCheckState(); return this.chkAll.CheckState; } /// /// 选中指定项目 /// /// 要为其设置复选状态的项 /// 若要将该项设置为选中,则为 true;否则为 false public virtual void SetItemCheckedByItem(bool value, params object[] items) { this.cklItem.SetItemCheckedByItem(value, items); } /// /// 选中指定值的项目 /// /// 要为其设置复选状态的项的值 /// 若要将该项设置为选中,则为 true;否则为 false public virtual void SetItemCheckedByValue(bool value, params object[] itemValues) { this.cklItem.SetItemCheckedByValue(value, itemValues); } /// /// 将指定索引处的项的 System.Windows.Forms.CheckState 设置为 Checked /// /// 若要将该项设置为选中,则为 true;否则为 false /// 要为其设置复选状态的项的索引 public void SetItemChecked(bool value, params int[] indexs) { this.cklItem.SetItemChecked(value, indexs); } /* /// /// 设置指定索引处项的复选状态 /// /// 要为其设置状态的项的索引 /// public void SetItemChecked(int index, bool value) { this.clstItem.SetItemChecked(index, value); } /// /// 设置指定索引处项的复选状态 /// /// 要为其设置状态的项的索引 /// System.Windows.Forms.CheckState 值之一 public void SetItemCheckState(int index, CheckState value) { this.clstItem.SetItemChecked(index, value); } */ #endregion #endregion #endregion #region 属性 #region CustomerEx /// /// 获取或设置全选CheckBox的Text。 /// [Description("获取或设置全选CheckBox的Text。"), Category("CustomerEx")] [DefaultValue("全部")] public string TextAll { get { return this.chkAll.Text; } set { this.chkAll.Text = value; } } /// /// 获取或设置一个值,该值指示此全选是否允许三种复选状态而不是两种。 /// [Description("获取或设置一个值,该值指示此全选是否允许三种复选状态而不是两种。"), Category("CustomerEx")] [DefaultValue(false)] public bool ThreeState { get { return this.chkAll.ThreeState; } set { this.chkAll.ThreeState = value; if (!value) { this._indeterminateIndices.Clear(); } } } /// /// 获取或设置控件的背景色。 /// //[Description("获取或设置控件的背景色。"), Category("CustomerEx")] [DefaultValue(typeof(Color), "Window")] public override Color BackColor { get { Color bc = base.BackColor; if (bc.IsEmpty) { return SystemColors.Window; } return bc; } set { if (value.IsEmpty) { base.BackColor = SystemColors.Window; } else { base.BackColor = value; } } } /// /// 获取或设置用户控件的边框样式 /// //[Description("获取或设置用户控件的边框样式。"), Category("CustomerEx")] [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [DefaultValue(typeof(BorderStyle), "FixedSingle")] public new BorderStyle BorderStyle { get { return base.BorderStyle; } set { base.BorderStyle = value; } } /// /// 获取或设置全部项目的选中状态。 /// [DefaultValue(typeof(CheckState), "Unchecked")] [Description("获取或设置全部项目的选中状态。"), Category("CustomerEx")] public CheckState CheckedState { get { return this.chkAll.CheckState; } set { this.chkAll.CheckState = value; } } #endregion #endregion #region 重写事件 #region 属性改变 /// /// 引发 HasErrorChanged 事件 /// /// 包含事件数据的 EventArgs protected virtual void OnHasErrorChanged(EventArgs e) { EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged]; if (eventHandler != null) { eventHandler(this, e); } } /// /// 字体改变事件 /// /// protected override void OnFontChanged(EventArgs e) { base.OnAutoSizeChanged(e); this.chkAll.Font = this.Font; this.cklItem.Font = this.Font; } /// /// 背景色改变事件 /// /// protected override void OnBackColorChanged(EventArgs e) { base.OnBackColorChanged(e); this.pnlAll.BackColor = this.BackColor; this.pnlItem.BackColor = this.BackColor; this.chkAll.BackColor = this.BackColor; this.cklItem.BackColor = this.BackColor; } /// /// 字体颜色改变 /// /// protected override void OnForeColorChanged(EventArgs e) { base.OnForeColorChanged(e); this.pnlAll.ForeColor = this.ForeColor; this.pnlItem.ForeColor = this.ForeColor; this.chkAll.ForeColor = this.ForeColor; this.cklItem.ForeColor = this.ForeColor; } /// /// ImeMode改变 /// /// protected override void OnImeModeChanged(EventArgs e) { base.OnImeModeChanged(e); this.pnlAll.ImeMode = this.ImeMode; this.pnlItem.ImeMode = this.ImeMode; this.chkAll.ImeMode = this.ImeMode; this.cklItem.ImeMode = this.ImeMode; } /// /// RightToLeft改变 /// /// protected override void OnRightToLeftChanged(EventArgs e) { base.OnRightToLeftChanged(e); this.pnlAll.RightToLeft = this.RightToLeft; this.pnlItem.RightToLeft = this.RightToLeft; this.chkAll.RightToLeft = this.RightToLeft; this.cklItem.RightToLeft = this.RightToLeft; } #endregion #region 键盘事件 #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 重写方法 /// /// 处理 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 事件处理 /// /// 获得焦点 /// /// /// private void cklItem_Enter(object sender, EventArgs e) { this.OnEnter(e); } /// /// 焦点离开 /// /// /// private void cklItem_Leave(object sender, EventArgs e) { this.OnLeave(e); } /// /// 鼠标进入控件 /// /// /// private void cklItem_MouseEnter(object sender, EventArgs e) { this.OnMouseEnter(e); } /// /// 鼠标离开控件 /// /// /// private void cklItem_MouseLeave(object sender, EventArgs e) { this.OnMouseLeave(e); } /// /// 全部选择 /// /// /// private void pnlAll_Click(object sender, EventArgs e) { if (this.chkAll.ThreeState) { int cs = (int)this.chkAll.CheckState + 1; cs = cs > 2 ? 0 : cs; this.chkAll.CheckState = (CheckState)cs; } else { this.chkAll.Checked = !this.chkAll.Checked; } } /// /// 全部选择状态改变 /// /// /// private void chkAll_CheckedChanged(object sender, EventArgs e) { //if (this._isCheckAll.HasValue && !this._isCheckAll.Value) //{ // return; //} //this._isCheckAll = true; //if (this._mustInput) //{ // CheckState cs = this.cklItem.GetItemsCheckState(); // if (cs != CheckState.Checked) // { // this.cklItem.SetItemsChecked(true); // } // this._isCheckAll = false; // this.chkAll.Checked = true; // this._isCheckAll = null; // this.ValidateData(); // return; //} //this.cklItem.SetItemsChecked(this.chkAll.Checked); //this._isCheckAll = null; } /// /// 全部选择状态改变 /// /// /// private void chkAll_CheckStateChanged(object sender, EventArgs e) { if (this._isCheckAll.HasValue) { return; } if (this.cklItem.Items.Count == 0) { this._isCheckAll = false; this.chkAll.Checked = false; this._isCheckAll = null; return; } this._isCheckAll = true; switch (this.chkAll.CheckState) { case CheckState.Checked: if (this.cklItem.SimpleChecked) { this._isCheckAll = null; this.cklItem.ResetMustOrSimpleChecked(); this._isCheckAll = false; this.chkAll.CheckState = this.cklItem.GetItemsCheckState(); this._isCheckAll = null; return; } else { this.cklItem.SetItemsChecked(true); } break; case CheckState.Unchecked: if (this._mustInput) { this._isCheckAll = null; this.chkAll.Checked = true; return; } this.cklItem.SetItemsChecked(false); break; case CheckState.Indeterminate: if (this.cklItem.SimpleChecked) { this._isCheckAll = null; this.cklItem.ResetMustOrSimpleChecked(); this._isCheckAll = false; this.chkAll.CheckState = this.cklItem.GetItemsCheckState(); this._isCheckAll = null; return; } else { if (this._indeterminateIndices != null && this._indeterminateIndices.Count > 0) { this._isCheckAll = true; for (int i = 0; i < this.cklItem.Items.Count; i++) { this.SetItemChecked(this._indeterminateIndices.Contains(i), i); } } else { this._isCheckAll = null; this.chkAll.Checked = false; return; } } break; default: break; } this._isCheckAll = null; this.ValidateData(); } /// /// 项目选择状态改变 /// /// /// private void cklItem_ItemChecked(object sender, ItemCheckEventArgs e) { if (this._isCheckAll.HasValue) { return; } this._isCheckAll = false; this.chkAll.CheckState = this.cklItem.GetItemsCheckState(); if (this.chkAll.ThreeState) { // 点击项目变成全选状态时,是否记录-不记录。 //this._indeterminateIndices.Clear(); if (this.chkAll.CheckState == CheckState.Indeterminate) { // 点击项目变成全选状态时,是否记录-记录。 this._indeterminateIndices.Clear(); foreach (int checkedIndex in this.cklItem.CheckedIndices) { this._indeterminateIndices.Add(checkedIndex); } } } this._isCheckAll = null; if (this._mustInput) { this.ValidateData(); } } /// /// 验证有错误时 /// /// /// private void cklItem_HasErrorChanged(object sender, EventArgs e) { this.OnHasErrorChanged(e); this.InvalidateBorder(); } /// /// 键盘按下事件 /// /// /// private void chkAll_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { switch (e.KeyCode) { case Keys.Up: case Keys.Left: case Keys.Down: case Keys.Right: if (this.cklItem.Items.Count > 0) { this.cklItem.Focus(); if (this.cklItem.SelectedIndex == -1) { this.cklItem.SelectedIndex = 0; } } break; default: break; } } /// /// 在移动鼠标滚轮并且控件有焦点时发生 /// /// /// private void chkAll_MouseWheel(object sender, MouseEventArgs e) { //if (e.Delta > 0) //{ // this.lstItem.SelectPrevItem(); //} //else //{ // this.lstItem.SelectNextItem(); //} if (this.cklItem.Items.Count > 0) { this.cklItem.Focus(); if (this.cklItem.SelectedIndex == -1) { this.cklItem.SelectedIndex = 0; } } } /// /// 无选择项目 /// /// /// private void cklItem_SelectedIndexChanged(object sender, EventArgs e) { if (this.cklItem.SelectedIndex == -1) { this.chkAll.Focus(); } } /// /// 全选checkbox获得焦点 /// /// /// private void chkAll_GotFocus(object sender, EventArgs e) { this.cklItem.SelectedIndex = -1; } #endregion #region 公有方法 /// /// 第一个控件获得焦点 /// public void FocusOnShown() { this.chkAll.Focus(); } #endregion #region 保护方法 /// /// 设置Item选中状态 /// protected virtual void ResetMustChecked() { this.cklItem.ResetMustOrSimpleChecked(); } #endregion #region 内部方法 #endregion #region 私有方法 /// /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。 /// 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.cklItem.MustInput = value; 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 || this.cklItem.HasError; } } /// /// 获取控件校验时的错误编码 /// [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")] [DefaultValue(typeof(ControlErrorCode), "DKC_0000")] public ControlErrorCode ErrorCode { get { //return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode; return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this.cklItem.ErrorCode; } } /// /// 获取控件校验时的错误消息 /// [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")] [DefaultValue(null)] public string ErrorMessage { get { //return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage; return this._hasCustomerError ? this._customerErrorMessage : this.cklItem.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() { //bool result = this.cklItem.ValidateData(); //this.InvalidateBorder(); //return result; if (this._mustInput && this.CheckedState == CheckState.Unchecked) { this.SetError(true, ControlErrorCode.DKC_0001, this.CDItemName); return false; } this.ClearError(); return true; } /// /// 清除输入项 /// public virtual void ClearValue() { this.chkAll.Checked = false; this._indeterminateIndices.Clear(); } #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; private Control _activeControl = null; #endregion #region 公有方法 /// /// 开始异步处理 /// /// 是否处理焦点 public virtual void BeginAsync(ref bool doFocus) { this._asyncBeginFocused = false; //if (doFocus && this.Focused) if (doFocus && this.ActiveControl != null) { this._asyncBeginFocused = true; this._activeControl = this.ActiveControl; doFocus = false; } this._asyncBeginStatus = this.Enabled; this.Enabled = false; } /// /// 结束异步处理 /// public virtual void EndAsync() { this.Enabled = this._asyncBeginStatus; if (this._asyncBeginFocused) { //this.Focus(); this.ActiveControl = this._activeControl; this._activeControl = null; } } #endregion #endregion } }