C_CheckedListBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:C_CheckedListBox.cs
  5. * 2.功能描述:扩展的复选框列表控件:便于修改背景颜色及字体、颜色
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.Drawing;
  13. using System.Security.Permissions;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  17. {
  18. /// <summary>
  19. /// 扩展的复选框列表控件
  20. /// </summary>
  21. public partial class C_CheckedListBox : CheckedListBox
  22. {
  23. #region 成员变量
  24. private CheckState _selectedItemCheckState = CheckState.Unchecked;
  25. private Object _selectedItem = null;
  26. private bool _canMoveItem = false;
  27. #endregion
  28. #region 构造函数
  29. public C_CheckedListBox()
  30. {
  31. InitializeComponent();
  32. }
  33. #endregion
  34. #region 属性
  35. /// <summary>
  36. /// 获取或者设定列表的项目是否可以移动
  37. /// </summary>
  38. [DefaultValue(false)]
  39. public bool CanMoveItem
  40. {
  41. get
  42. {
  43. return _canMoveItem;
  44. }
  45. set
  46. {
  47. _canMoveItem = value;
  48. }
  49. }
  50. /// <summary>
  51. /// 获取或者设定选中的项目的数组
  52. /// </summary>
  53. public Object[] SelectedValues
  54. {
  55. get
  56. {
  57. System.Collections.Generic.List<Object> list =
  58. new System.Collections.Generic.List<Object>();
  59. foreach (CheckedBoxListItem item in this.CheckedItems)
  60. {
  61. list.Add(item.Value);
  62. }
  63. return list.ToArray();
  64. }
  65. set
  66. {
  67. if (value != null)
  68. {
  69. SetItemCheck(value);
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 获取或者设定选中的项目的集合例如返回1,2,3
  75. /// </summary>
  76. [Description("获取或者设定选中的项目的集合.例如返回\"1,2,3\"")]
  77. public string SelectedArray
  78. {
  79. get
  80. {
  81. string array = "";
  82. foreach (CheckedBoxListItem item in this.CheckedItems)
  83. {
  84. array += "," + item.Value;
  85. }
  86. array = array.Substring(1);
  87. return array;
  88. }
  89. }
  90. #endregion
  91. #region 公开方法或者函数
  92. /// <summary>
  93. /// 往控件的项目中追加项目
  94. /// </summary>
  95. /// <param name="item">追加的项目</param>
  96. public void Add(CheckedBoxListItem item)
  97. {
  98. base.Items.Add(item);
  99. item.ParentControl = this;
  100. }
  101. /// <summary>
  102. /// 清除所有的选中状态
  103. /// </summary>
  104. public void ClearItemCheck()
  105. {
  106. for (int i = this.Items.Count - 1; 0 <= i; i--)
  107. {
  108. this.SetItemCheckState(i, CheckState.Unchecked);
  109. CheckedBoxListItem item = this.Items[i] as CheckedBoxListItem;
  110. if (item == null)
  111. {
  112. continue;
  113. }
  114. item.Checked = CheckState.Unchecked;
  115. }
  116. }
  117. /// <summary>
  118. /// 将所有项目设置成选中状态
  119. /// </summary>
  120. public void AllItemCheck()
  121. {
  122. for (int i = this.Items.Count - 1; 0 <= i; i--)
  123. {
  124. this.SetItemCheckState(i, CheckState.Checked);
  125. CheckedBoxListItem item = this.Items[i] as CheckedBoxListItem;
  126. if (item == null)
  127. {
  128. continue;
  129. }
  130. item.Checked = CheckState.Checked;
  131. }
  132. }
  133. #endregion
  134. #region 受保护的方法或者函数
  135. /// <summary>
  136. /// 将指定的项目设置成选中状态
  137. /// </summary>
  138. /// <param name="selectedValues">指定选中的项目</param>
  139. protected void SetItemCheck(Object[] selectedValues)
  140. {
  141. ClearItemCheck();
  142. for (int k = 0; k < selectedValues.Length; k++)
  143. {
  144. for (int i = this.Items.Count - 1; 0 <= i; i--)
  145. {
  146. CheckedBoxListItem item = this.Items[i] as CheckedBoxListItem;
  147. if (item == null)
  148. {
  149. continue;
  150. }
  151. if (selectedValues[k].ToString().Equals(item.Value.ToString()))
  152. {
  153. this.SetItemCheckState(i, CheckState.Checked);
  154. item.Checked = CheckState.Checked;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. #endregion
  161. #region 私有方法或者函数
  162. /// <summary>
  163. /// 控件的项目选中时的事件
  164. /// </summary>
  165. /// <param name="sender"></param>
  166. /// <param name="e"></param>
  167. private void CheckedListBox_ItemCheck(Object sender, ItemCheckEventArgs e)
  168. {
  169. if (this.Items[e.Index] is CheckedBoxListItem)
  170. {
  171. CheckedBoxListItem item = (CheckedBoxListItem)this.Items[e.Index];
  172. item.Checked = e.NewValue;
  173. }
  174. }
  175. #endregion
  176. #region 控件的事件
  177. /// <summary>
  178. /// 项目值选中状态事件
  179. /// </summary>
  180. /// <param name="e"></param>
  181. protected override void OnItemCheck(ItemCheckEventArgs e)
  182. {
  183. if (this.Items[e.Index] is CheckedBoxListItem)
  184. {
  185. CheckedBoxListItem item = (CheckedBoxListItem)this.Items[e.Index];
  186. if (item.Checked == e.NewValue)
  187. {
  188. return;
  189. }
  190. base.OnItemCheck(e);
  191. item.Checked = e.NewValue;
  192. }
  193. else
  194. {
  195. base.OnItemCheck(e);
  196. }
  197. }
  198. /// <summary>
  199. /// 鼠标点击事件,做移动处理
  200. /// </summary>
  201. /// <param name="e"></param>
  202. protected override void OnMouseDown(MouseEventArgs e)
  203. {
  204. base.OnMouseDown(e);
  205. if (0 <= this.SelectedIndex && e.Button == MouseButtons.Left)
  206. {
  207. _selectedItem = this.SelectedItem;
  208. _selectedItemCheckState = this.GetItemCheckState(this.SelectedIndex);
  209. this.ClearSelected();
  210. this.SelectedItem = _selectedItem;
  211. }
  212. }
  213. /// <summary>
  214. /// 鼠标移动事件,主要是项目移动
  215. /// </summary>
  216. /// <param name="e"></param>
  217. protected override void OnMouseUp(MouseEventArgs e)
  218. {
  219. base.OnMouseUp(e);
  220. if (_selectedItem != null)
  221. {
  222. if (_selectedItem == this.SelectedItem)
  223. {
  224. }
  225. else
  226. {
  227. if (0 <= this.SelectedIndex && this.CanMoveItem)
  228. {
  229. this.Items.Remove(_selectedItem);
  230. this.Items.Insert(this.SelectedIndex, _selectedItem);
  231. this.SetItemCheckState(this.SelectedIndex - 1, _selectedItemCheckState);
  232. }
  233. }
  234. }
  235. }
  236. #endregion
  237. }
  238. /// <summary>
  239. /// 控件的项目类
  240. /// </summary>
  241. public class CheckedBoxListItem
  242. {
  243. #region 成员变量
  244. // 选中项目的内容
  245. private string _displayString = string.Empty;
  246. // 选中项目的状态
  247. private CheckState _checked = CheckState.Unchecked;
  248. // 选中项目的值
  249. private Object _value = null;
  250. // ParentControl
  251. private C_CheckedListBox _parentControl = null;
  252. // 防止死循环
  253. private bool _canSynchronizeFlag = false;
  254. #endregion
  255. #region 构造函数
  256. /// <summary>
  257. /// 构造函数
  258. /// </summary>
  259. public CheckedBoxListItem()
  260. {
  261. }
  262. /// <summary>
  263. /// 控件的项目类的构造函数
  264. /// </summary>
  265. /// <param name="displayString">选中项目的内容</param>
  266. /// <param name="itemChecked">选中项目的状态</param>
  267. /// <param name="value">选中项目的值</param>
  268. public CheckedBoxListItem(string displayString, CheckState itemChecked, Object value)
  269. {
  270. _displayString = displayString;
  271. _checked = itemChecked;
  272. _value = value;
  273. }
  274. #endregion
  275. #region 属性
  276. /// <summary>
  277. /// 获取或者设定选中项目的内容
  278. /// </summary>
  279. public string DisplayString
  280. {
  281. get
  282. {
  283. return _displayString;
  284. }
  285. set
  286. {
  287. _displayString = value;
  288. }
  289. }
  290. /// <summary>
  291. /// 获取或者设定项目的选中状态
  292. /// </summary>
  293. public CheckState Checked
  294. {
  295. get
  296. {
  297. return _checked;
  298. }
  299. set
  300. {
  301. _checked = value;
  302. this.SetItemChecked();
  303. }
  304. }
  305. /// <summary>
  306. /// 获取或者设定项目的值
  307. /// </summary>
  308. public Object Value
  309. {
  310. get
  311. {
  312. return _value;
  313. }
  314. set
  315. {
  316. _value = value;
  317. }
  318. }
  319. /// <summary>
  320. /// 获取或者设定ParentControl属性
  321. /// </summary>
  322. public C_CheckedListBox ParentControl
  323. {
  324. get
  325. {
  326. return _parentControl;
  327. }
  328. set
  329. {
  330. _parentControl = value;
  331. this.SetItemChecked();
  332. }
  333. }
  334. #endregion
  335. #region 公开方法或者函数
  336. /// <summary>
  337. /// 选中项目的内容
  338. /// </summary>
  339. /// <returns>选中项目的内容</returns>
  340. public override string ToString()
  341. {
  342. return _displayString;
  343. }
  344. #endregion
  345. #region 受保护的方法或者函数
  346. /// <summary>
  347. /// 设定各项目的选中状态
  348. /// </summary>
  349. protected void SetItemChecked()
  350. {
  351. if (this.ParentControl == null)
  352. {
  353. return;
  354. }
  355. int index = this.ParentControl.Items.IndexOf(this);
  356. if (0 <= index
  357. && this.ParentControl.GetItemCheckState(index) != this.Checked
  358. && !_canSynchronizeFlag)
  359. {
  360. _canSynchronizeFlag = true;
  361. this.ParentControl.SetItemCheckState(index, this.Checked);
  362. _canSynchronizeFlag = false;
  363. }
  364. }
  365. #endregion
  366. }
  367. }