/******************************************************************************* * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential * 类的信息: * 1.程序名称:C_CheckedListBox.cs * 2.功能描述:扩展的复选框列表控件:便于修改背景颜色及字体、颜色 * 编辑履历: * 作者 日期 版本 修改内容 * 陈晓野 2014/08/13 1.00 新建 *******************************************************************************/ using System; using System.ComponentModel; using System.Drawing; using System.Security.Permissions; using System.Text; using System.Windows.Forms; namespace Dongke.IBOSS.PRD.Basics.BaseControls { /// /// 扩展的复选框列表控件 /// public partial class C_CheckedListBox : CheckedListBox { #region 成员变量 private CheckState _selectedItemCheckState = CheckState.Unchecked; private Object _selectedItem = null; private bool _canMoveItem = false; #endregion #region 构造函数 public C_CheckedListBox() { InitializeComponent(); } #endregion #region 属性 /// /// 获取或者设定列表的项目是否可以移动 /// [DefaultValue(false)] public bool CanMoveItem { get { return _canMoveItem; } set { _canMoveItem = value; } } /// /// 获取或者设定选中的项目的数组 /// public Object[] SelectedValues { get { System.Collections.Generic.List list = new System.Collections.Generic.List(); foreach (CheckedBoxListItem item in this.CheckedItems) { list.Add(item.Value); } return list.ToArray(); } set { if (value != null) { SetItemCheck(value); } } } /// /// 获取或者设定选中的项目的集合例如返回1,2,3 /// [Description("获取或者设定选中的项目的集合.例如返回\"1,2,3\"")] public string SelectedArray { get { string array = ""; foreach (CheckedBoxListItem item in this.CheckedItems) { array += "," + item.Value; } array = array.Substring(1); return array; } } #endregion #region 公开方法或者函数 /// /// 往控件的项目中追加项目 /// /// 追加的项目 public void Add(CheckedBoxListItem item) { base.Items.Add(item); item.ParentControl = this; } /// /// 清除所有的选中状态 /// public void ClearItemCheck() { for (int i = this.Items.Count - 1; 0 <= i; i--) { this.SetItemCheckState(i, CheckState.Unchecked); CheckedBoxListItem item = this.Items[i] as CheckedBoxListItem; if (item == null) { continue; } item.Checked = CheckState.Unchecked; } } /// /// 将所有项目设置成选中状态 /// public void AllItemCheck() { for (int i = this.Items.Count - 1; 0 <= i; i--) { this.SetItemCheckState(i, CheckState.Checked); CheckedBoxListItem item = this.Items[i] as CheckedBoxListItem; if (item == null) { continue; } item.Checked = CheckState.Checked; } } #endregion #region 受保护的方法或者函数 /// /// 将指定的项目设置成选中状态 /// /// 指定选中的项目 protected void SetItemCheck(Object[] selectedValues) { ClearItemCheck(); for (int k = 0; k < selectedValues.Length; k++) { for (int i = this.Items.Count - 1; 0 <= i; i--) { CheckedBoxListItem item = this.Items[i] as CheckedBoxListItem; if (item == null) { continue; } if (selectedValues[k].ToString().Equals(item.Value.ToString())) { this.SetItemCheckState(i, CheckState.Checked); item.Checked = CheckState.Checked; break; } } } } #endregion #region 私有方法或者函数 /// /// 控件的项目选中时的事件 /// /// /// private void CheckedListBox_ItemCheck(Object sender, ItemCheckEventArgs e) { if (this.Items[e.Index] is CheckedBoxListItem) { CheckedBoxListItem item = (CheckedBoxListItem)this.Items[e.Index]; item.Checked = e.NewValue; } } #endregion #region 控件的事件 /// /// 项目值选中状态事件 /// /// protected override void OnItemCheck(ItemCheckEventArgs e) { if (this.Items[e.Index] is CheckedBoxListItem) { CheckedBoxListItem item = (CheckedBoxListItem)this.Items[e.Index]; if (item.Checked == e.NewValue) { return; } base.OnItemCheck(e); item.Checked = e.NewValue; } else { base.OnItemCheck(e); } } /// /// 鼠标点击事件,做移动处理 /// /// protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (0 <= this.SelectedIndex && e.Button == MouseButtons.Left) { _selectedItem = this.SelectedItem; _selectedItemCheckState = this.GetItemCheckState(this.SelectedIndex); this.ClearSelected(); this.SelectedItem = _selectedItem; } } /// /// 鼠标移动事件,主要是项目移动 /// /// protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (_selectedItem != null) { if (_selectedItem == this.SelectedItem) { } else { if (0 <= this.SelectedIndex && this.CanMoveItem) { this.Items.Remove(_selectedItem); this.Items.Insert(this.SelectedIndex, _selectedItem); this.SetItemCheckState(this.SelectedIndex - 1, _selectedItemCheckState); } } } } #endregion } /// /// 控件的项目类 /// public class CheckedBoxListItem { #region 成员变量 // 选中项目的内容 private string _displayString = string.Empty; // 选中项目的状态 private CheckState _checked = CheckState.Unchecked; // 选中项目的值 private Object _value = null; // ParentControl private C_CheckedListBox _parentControl = null; // 防止死循环 private bool _canSynchronizeFlag = false; #endregion #region 构造函数 /// /// 构造函数 /// public CheckedBoxListItem() { } /// /// 控件的项目类的构造函数 /// /// 选中项目的内容 /// 选中项目的状态 /// 选中项目的值 public CheckedBoxListItem(string displayString, CheckState itemChecked, Object value) { _displayString = displayString; _checked = itemChecked; _value = value; } #endregion #region 属性 /// /// 获取或者设定选中项目的内容 /// public string DisplayString { get { return _displayString; } set { _displayString = value; } } /// /// 获取或者设定项目的选中状态 /// public CheckState Checked { get { return _checked; } set { _checked = value; this.SetItemChecked(); } } /// /// 获取或者设定项目的值 /// public Object Value { get { return _value; } set { _value = value; } } /// /// 获取或者设定ParentControl属性 /// public C_CheckedListBox ParentControl { get { return _parentControl; } set { _parentControl = value; this.SetItemChecked(); } } #endregion #region 公开方法或者函数 /// /// 选中项目的内容 /// /// 选中项目的内容 public override string ToString() { return _displayString; } #endregion #region 受保护的方法或者函数 /// /// 设定各项目的选中状态 /// protected void SetItemChecked() { if (this.ParentControl == null) { return; } int index = this.ParentControl.Items.IndexOf(this); if (0 <= index && this.ParentControl.GetItemCheckState(index) != this.Checked && !_canSynchronizeFlag) { _canSynchronizeFlag = true; this.ParentControl.SetItemCheckState(index, this.Checked); _canSynchronizeFlag = false; } } #endregion } }