CheckedListBoxControl.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using FastReport.Utils;
  5. using System.Windows.Forms;
  6. using FastReport.Data;
  7. namespace FastReport.Dialog
  8. {
  9. /// <summary>
  10. /// Displays a ListBox in which a check box is displayed to the left of each item.
  11. /// Wraps the <see cref="System.Windows.Forms.CheckedListBox"/> control.
  12. /// </summary>
  13. public partial class CheckedListBoxControl : ListBoxBaseControl
  14. {
  15. private CheckedListBox checkedListBox;
  16. private string itemCheckEvent;
  17. private Timer timer;
  18. #region Properties
  19. /// <summary>
  20. /// Occurs after item's check state was changed.
  21. /// Wraps the <see cref="System.Windows.Forms.CheckedListBox.ItemCheck"/> event.
  22. /// </summary>
  23. public event ItemCheckEventHandler ItemCheck;
  24. /// <summary>
  25. /// Gets an internal <b>CheckedListBox</b>.
  26. /// </summary>
  27. [Browsable(false)]
  28. public CheckedListBox CheckedListBox
  29. {
  30. get { return checkedListBox; }
  31. }
  32. /// <summary>
  33. /// Gets or sets a value indicating whether the check box should be toggled when an item is selected.
  34. /// Wraps the <see cref="System.Windows.Forms.CheckedListBox.CheckOnClick"/> property.
  35. /// </summary>
  36. [DefaultValue(false)]
  37. public bool CheckOnClick
  38. {
  39. get { return CheckedListBox.CheckOnClick; }
  40. set { CheckedListBox.CheckOnClick = value; }
  41. }
  42. /// <summary>
  43. /// Gets the items of the CheckedListBox.
  44. /// Wraps the <see cref="System.Windows.Forms.CheckedListBox.Items"/> property.
  45. /// </summary>
  46. public new CheckedListBox.ObjectCollection Items
  47. {
  48. get { return CheckedListBox.Items; }
  49. }
  50. /// <summary>
  51. /// Collection of checked indexes in this CheckedListBox.
  52. /// Wraps the <see cref="System.Windows.Forms.CheckedListBox.CheckedIndices"/> property.
  53. /// </summary>
  54. [Browsable(false)]
  55. public CheckedListBox.CheckedIndexCollection CheckedIndices
  56. {
  57. get { return CheckedListBox.CheckedIndices; }
  58. }
  59. /// <summary>
  60. /// Collection of checked items in this CheckedListBox.
  61. /// Wraps the <see cref="System.Windows.Forms.CheckedListBox.CheckedItems"/> property.
  62. /// </summary>
  63. [Browsable(false)]
  64. public CheckedListBox.CheckedItemCollection CheckedItems
  65. {
  66. get { return CheckedListBox.CheckedItems; }
  67. }
  68. /// <inheritdoc/>
  69. public override string ItemsText
  70. {
  71. get { return Converter.IListToString(Items); }
  72. set { Converter.StringToIList(value, Items); }
  73. }
  74. /// <summary>
  75. /// Gets or sets a script method name that will be used to handle the
  76. /// <see cref="ItemCheck"/> event.
  77. /// </summary>
  78. [Category("Events")]
  79. public string ItemCheckEvent
  80. {
  81. get { return itemCheckEvent; }
  82. set { itemCheckEvent = value; }
  83. }
  84. #endregion
  85. #region Private Methods
  86. private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
  87. {
  88. OnItemCheck(e);
  89. }
  90. private void FTimer_Tick(object sender, EventArgs e)
  91. {
  92. timer.Stop();
  93. OnFilterChanged();
  94. }
  95. #endregion
  96. #region Protected Methods
  97. /// <inheritdoc/>
  98. protected override void Dispose(bool disposing)
  99. {
  100. base.Dispose(disposing);
  101. timer.Dispose();
  102. }
  103. /// <inheritdoc/>
  104. protected override void AttachEvents()
  105. {
  106. base.AttachEvents();
  107. CheckedListBox.ItemCheck += new ItemCheckEventHandler(CheckedListBox_ItemCheck);
  108. }
  109. /// <inheritdoc/>
  110. protected override void DetachEvents()
  111. {
  112. base.DetachEvents();
  113. CheckedListBox.ItemCheck -= new ItemCheckEventHandler(CheckedListBox_ItemCheck);
  114. }
  115. /// <inheritdoc/>
  116. protected override void FillData(DataSourceBase dataSource, Column column)
  117. {
  118. Items.Clear();
  119. Items.AddRange(GetListOfData(dataSource, column));
  120. if (Items.Count > 0)
  121. SelectedIndex = 0;
  122. }
  123. /// <inheritdoc/>
  124. protected override object GetValue()
  125. {
  126. List<string> list = new List<string>();
  127. foreach (object item in CheckedItems)
  128. {
  129. list.Add((string)item);
  130. }
  131. return list.ToArray();
  132. }
  133. #endregion
  134. #region Public Methods
  135. /// <inheritdoc/>
  136. public override void Serialize(FRWriter writer)
  137. {
  138. CheckedListBoxControl c = writer.DiffObject as CheckedListBoxControl;
  139. base.Serialize(writer);
  140. if (CheckOnClick != c.CheckOnClick)
  141. writer.WriteBool("CheckOnClick", CheckOnClick);
  142. if (ItemCheckEvent != c.ItemCheckEvent)
  143. writer.WriteStr("ItemCheckEvent", ItemCheckEvent);
  144. }
  145. /// <summary>
  146. /// This method fires the <b>ItemCheck</b> event and the script code connected to the <b>ItemCheckEvent</b>.
  147. /// </summary>
  148. /// <param name="e">Event data.</param>
  149. public virtual void OnItemCheck(ItemCheckEventArgs e)
  150. {
  151. if (ItemCheck != null)
  152. ItemCheck(this, e);
  153. InvokeEvent(ItemCheckEvent, e);
  154. timer.Start();
  155. }
  156. #endregion
  157. /// <summary>
  158. /// Initializes a new instance of the <b>CheckedListBoxControl</b> class with default settings.
  159. /// </summary>
  160. public CheckedListBoxControl()
  161. {
  162. checkedListBox = new CheckedListBox();
  163. Control = checkedListBox;
  164. CheckedListBox.IntegralHeight = false;
  165. timer = new Timer();
  166. timer.Interval = 50;
  167. timer.Tick += new EventHandler(FTimer_Tick);
  168. }
  169. }
  170. }