CkcCheckedComboBox.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. 
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Design;
  6. using System.Windows.Forms;
  7. namespace Dongke.WinForm.Controls
  8. {
  9. /// <summary>
  10. /// 多选下拉框控件
  11. /// </summary>
  12. [ToolboxBitmap(typeof(ComboBox))]
  13. public partial class CkcCheckedComboBox : PopupComboBox
  14. {
  15. #region 成员变量
  16. /// <summary>
  17. /// 全选ListBox
  18. /// </summary>
  19. private CalCheckAllListBox _calListBox = new CalCheckAllListBox();
  20. #endregion
  21. #region 构造函数
  22. /// <summary>
  23. /// 多选下拉框控件
  24. /// </summary>
  25. public CkcCheckedComboBox()
  26. {
  27. base.FormattingEnabled = false;
  28. this._calListBox.ShowMustInputAlert = false;
  29. this._calListBox.InputErrorAlert = InputErrorAlert.None;
  30. this._calListBox.ShowBorderColor = false;
  31. this._calListBox.BorderStyle = BorderStyle.None;
  32. this._calListBox.ItemChecked += CalListBox_ItemChecked;
  33. this.DropDownControl = this._calListBox;
  34. this.ShowComboBoxDropDown = false;
  35. }
  36. #endregion
  37. #region 属性
  38. /// <summary>
  39. /// 获取或设置全选CheckBox的Text。
  40. /// </summary>
  41. [Description("获取或设置全选CheckBox的Text。"), Category("CustomerEx")]
  42. [DefaultValue("全部")]
  43. public string TextAll
  44. {
  45. get
  46. {
  47. return this._calListBox.TextAll;
  48. }
  49. set
  50. {
  51. this._calListBox.TextAll = value;
  52. }
  53. }
  54. /// <summary>
  55. /// 获取或设置一个值,该值指示此全选是否允许三种复选状态而不是两种。
  56. /// </summary>
  57. [Description("获取或设置一个值,该值指示此全选是否允许三种复选状态而不是两种。"), Category("CustomerEx")]
  58. [DefaultValue(false)]
  59. public bool ThreeState
  60. {
  61. get
  62. {
  63. return this._calListBox.ThreeState;
  64. }
  65. set
  66. {
  67. this._calListBox.ThreeState = value;
  68. }
  69. }
  70. /// <summary>
  71. /// 获取选中的文本
  72. /// </summary>
  73. [Description("获取选中的文本。"), Category("CklCheckedListBox")]
  74. [DefaultValue(null)]
  75. public string[] CheckedTexts
  76. {
  77. get
  78. {
  79. return this._calListBox.CheckedTexts;
  80. }
  81. }
  82. /// <summary>
  83. /// 获取选中的值
  84. /// </summary>
  85. [Description("获取选中的值。"), Category("CklCheckedListBox")]
  86. [DefaultValue(null)]
  87. public object[] CheckedValues
  88. {
  89. get
  90. {
  91. return this._calListBox.CheckedValues;
  92. }
  93. }
  94. /// <summary>
  95. /// 获取选中项的文本表示形式,用【,】隔开。
  96. /// </summary>
  97. [Description("获取选中项的文本表示形式,用【,】隔开。"), Category("CklCheckedListBox")]
  98. [DefaultValue(null)]
  99. public string CheckedText
  100. {
  101. get
  102. {
  103. return this._calListBox.CheckedText;
  104. }
  105. }
  106. /// <summary>
  107. /// 获取选中项的值,用【,】隔开
  108. /// </summary>
  109. [Description("获取选中项的值,用【,】隔开。"), Category("CklCheckedListBox")]
  110. [DefaultValue(null)]
  111. public string CheckedValue
  112. {
  113. get
  114. {
  115. return this._calListBox.CheckedValue;
  116. }
  117. }
  118. ///// <summary>
  119. ///// 获取或设置控件是否单项选中。
  120. ///// </summary>
  121. //[Description("获取或设置控件是否单项选中。"), Category("CustomerEx")]
  122. //[DefaultValue(false)]
  123. //[Browsable(false)]
  124. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  125. //[EditorBrowsable(EditorBrowsableState.Never)]
  126. //public bool SimpleChecked
  127. //{
  128. // get
  129. // {
  130. // return this._calListBox.SimpleChecked;
  131. // }
  132. // set
  133. // {
  134. // this._calListBox.SimpleChecked = value;
  135. // }
  136. //}
  137. /// <summary>
  138. /// 该控件中选中索引的集合。
  139. /// </summary>
  140. [Description("该控件中选中索引的集合。"), Category("CheckedListBox")]
  141. [Browsable(false)]
  142. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  143. public CheckedListBox.CheckedIndexCollection CheckedIndices
  144. {
  145. get
  146. {
  147. return this._calListBox.CheckedIndices;
  148. }
  149. }
  150. /// <summary>
  151. /// 该控件中选中项的集合。
  152. /// </summary>
  153. [Description("该控件中选中项的集合。"), Category("CheckedListBox")]
  154. [Browsable(false)]
  155. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  156. public CheckedListBox.CheckedItemCollection CheckedItems
  157. {
  158. get
  159. {
  160. return this._calListBox.CheckedItems;
  161. }
  162. }
  163. /// <summary>
  164. /// 获取或设置一个值,该值指示当选定项时是否应切换复选框。
  165. /// </summary>
  166. [Description("获取或设置一个值,该值指示当选定项时是否应切换复选框。"), Category("CheckedListBox")]
  167. [DefaultValue(true)]
  168. public bool CheckOnClick
  169. {
  170. get
  171. {
  172. return this._calListBox.CheckOnClick;
  173. }
  174. set
  175. {
  176. this._calListBox.CheckOnClick = value;
  177. }
  178. }
  179. /// <summary>
  180. /// 获取或设置全部项目的选中状态。
  181. /// </summary>
  182. [DefaultValue(typeof(CheckState), "Unchecked")]
  183. [Description("获取或设置全部项目的选中状态。"), Category("CustomerEx")]
  184. public CheckState CheckedState
  185. {
  186. get
  187. {
  188. return this._calListBox.CheckedState;
  189. }
  190. set
  191. {
  192. this._calListBox.CheckedState = value;
  193. }
  194. }
  195. #endregion
  196. #region 重写属性
  197. /// <summary>
  198. /// 获取或设置控件的数据源
  199. /// </summary>
  200. [Description("获取或设置控件的数据源。"), Category("ListControl")]
  201. [AttributeProvider(typeof(IListSource))]
  202. [DefaultValue(null)]
  203. [RefreshProperties(RefreshProperties.Repaint)]
  204. public override object DataSource
  205. {
  206. get
  207. {
  208. return this._calListBox.DataSource;
  209. }
  210. set
  211. {
  212. this._calListBox.DataSource = value;
  213. }
  214. }
  215. /// <summary>
  216. /// 获取或设置控件的显示的属性
  217. /// </summary>
  218. [Description("获取或设置控件的显示的属性。"), Category("ListControl")]
  219. //[DefaultValue(null)]
  220. [DefaultValue("")]
  221. [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
  222. [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  223. public new string DisplayMember
  224. {
  225. get
  226. {
  227. return this._calListBox.DisplayMember;
  228. }
  229. set
  230. {
  231. this._calListBox.DisplayMember = value;
  232. }
  233. }
  234. /// <summary>
  235. /// 获取或设置一个属性,该属性将用作控件中的项的实际值。
  236. /// </summary>
  237. [Description("获取或设置一个属性,该属性将用作控件中的项的实际值。"), Category("ListControl")]
  238. //[DefaultValue(null)]
  239. [DefaultValue("")]
  240. [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
  241. public new string ValueMember
  242. {
  243. get
  244. {
  245. return this._calListBox.ValueMember;
  246. }
  247. set
  248. {
  249. this._calListBox.ValueMember = value;
  250. }
  251. }
  252. /// <summary>
  253. /// 获取该控件中项的集合。
  254. /// </summary>
  255. [Description("获取该控件中项的集合。"), Category("ListControl")]
  256. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  257. [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
  258. [Localizable(true)]
  259. [MergableProperty(false)]
  260. public new CheckedListBox.ObjectCollection Items
  261. {
  262. get
  263. {
  264. return this._calListBox.Items;
  265. }
  266. }
  267. /// <summary>
  268. /// 获取或设置控件是否必须选中项目。
  269. /// </summary>
  270. public override bool MustInput
  271. {
  272. get
  273. {
  274. return base.MustInput;
  275. }
  276. set
  277. {
  278. //this._calListBox.MustInput = value;
  279. base.MustInput = value;
  280. }
  281. }
  282. #endregion
  283. #region 重写事件
  284. #region 属性改变
  285. #endregion
  286. #region 行为事件
  287. #endregion
  288. #region 键盘事件
  289. #endregion
  290. #region 焦点事件
  291. #endregion
  292. #region 鼠标事件
  293. #endregion
  294. #endregion
  295. #region 重写方法
  296. /// <summary>
  297. /// 输入验证时,设定默认值。
  298. /// </summary>
  299. protected override void SetDefaultOnCheck()
  300. {
  301. //if (this._mustInput && this.SelectedIndex < 0 && this.Items.Count > 0)
  302. //{
  303. // this.SelectedIndex = 0;
  304. //}
  305. }
  306. /// <summary>
  307. /// 验证输入内容
  308. /// </summary>
  309. /// <returns>验证结果</returns>
  310. public override bool ValidateData()
  311. {
  312. if (this.MustInput && this.CheckedState == CheckState.Unchecked)
  313. {
  314. this.SetError(true, ControlErrorCode.DKC_0001, this.CDItemName);
  315. return false;
  316. }
  317. this.ClearError();
  318. return true;
  319. }
  320. /// <summary>
  321. /// 清除输入项
  322. /// </summary>
  323. public override void ClearValue()
  324. {
  325. this._calListBox.ClearValue();
  326. }
  327. #endregion
  328. #region 事件处理
  329. /// <summary>
  330. /// 项目选中事件
  331. /// </summary>
  332. /// <param name="sender"></param>
  333. /// <param name="e"></param>
  334. private void CalListBox_ItemChecked(object sender, ItemCheckedEventArgs e)
  335. {
  336. //this.Text = this._calListBox.CheckedText;
  337. this.SetText(this._calListBox.CheckedText);
  338. this.ValidateData();
  339. }
  340. #endregion
  341. #region 公有方法
  342. /// <summary>
  343. /// 获取指定项的成员属性的值
  344. /// </summary>
  345. /// <param name="item">项</param>
  346. /// <returns>值</returns>
  347. public virtual object GetItemValue(object item)
  348. {
  349. return this._calListBox.GetItemValue(item);
  350. }
  351. /// <summary>
  352. /// 获取选中项的文本表示形式
  353. /// </summary>
  354. /// <returns>选中项的文本</returns>
  355. public virtual List<string> GetCheckedItemTexts()
  356. {
  357. return this._calListBox.GetCheckedItemTexts();
  358. }
  359. /// <summary>
  360. /// 获取选中项的成员属性的值
  361. /// </summary>
  362. /// <returns>选中项的值</returns>
  363. public virtual List<object> GetCheckedItemValues()
  364. {
  365. return this._calListBox.GetCheckedItemValues();
  366. }
  367. /// <summary>
  368. /// 获取选中项的成员属性的值
  369. /// </summary>
  370. /// <typeparam name="T">属性值的类型</typeparam>
  371. /// <returns>选中项的值</returns>
  372. public virtual List<T> GetCheckedItemValues<T>()
  373. {
  374. return this._calListBox.GetCheckedItemValues<T>();
  375. }
  376. /// <summary>
  377. /// 设定全部项的复选状态
  378. /// </summary>
  379. /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
  380. public virtual void SetAllItemsChecked(bool value)
  381. {
  382. this._calListBox.SetAllItemsChecked(value);
  383. }
  384. /// <summary>
  385. /// 设置全部项目选中状态
  386. /// </summary>
  387. /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
  388. public virtual void SetAllItemsChecked(CheckState value)
  389. {
  390. this._calListBox.SetAllItemsChecked(value);
  391. }
  392. /// <summary>
  393. /// 获取全部项的复选状态
  394. /// </summary>
  395. /// <returns>选中状态</returns>
  396. public virtual CheckState GetItemsCheckState()
  397. {
  398. return this._calListBox.GetItemsCheckState();
  399. }
  400. /// <summary>
  401. /// 设定指定项的复选状态
  402. /// </summary>
  403. /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
  404. /// <param name="items">要为其设置复选状态的项</param>
  405. public virtual void SetItemCheckedByItem(bool value, params object[] items)
  406. {
  407. this._calListBox.SetItemCheckedByItem(value, items);
  408. }
  409. /// <summary>
  410. /// 设定指定值的项的复选状态
  411. /// </summary>
  412. /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
  413. /// <param name="itemValues">要为其设置复选状态的项的值</param>
  414. public virtual void SetItemCheckedByValue(bool value, params object[] itemValues)
  415. {
  416. this._calListBox.SetItemCheckedByValue(value, itemValues);
  417. }
  418. /// <summary>
  419. /// 设定指定索引处的项目选中或取消
  420. /// </summary>
  421. /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
  422. /// <param name="indexs">要为其设置复选状态的项的索引</param>
  423. public void SetItemChecked(bool value, params int[] indexs)
  424. {
  425. this._calListBox.SetItemCheckedByValue(value, indexs);
  426. }
  427. /// <summary>
  428. /// 设置指定索引处项的复选状态
  429. /// </summary>
  430. /// <param name="index">要为其设置状态的项的索引</param>
  431. /// <param name="value">若要将该项设置为选中,则为 true;否则为 false</param>
  432. public void SetItemChecked(int index, bool value)
  433. {
  434. this._calListBox.SetItemChecked(index, value);
  435. }
  436. /// <summary>
  437. /// 设置指定索引处项的复选状态
  438. /// </summary>
  439. /// <param name="index">要为其设置状态的项的索引</param>
  440. /// <param name="value">System.Windows.Forms.CheckState 值之一</param>
  441. public void SetItemCheckState(int index, CheckState value)
  442. {
  443. this._calListBox.SetItemCheckState(index, value);
  444. }
  445. #endregion
  446. #region 内部方法
  447. #endregion
  448. #region 保护方法
  449. #endregion
  450. #region 私有方法
  451. #endregion
  452. }
  453. }