CheckedListBoxControl.cs 5.2 KB

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