ListBoxBaseControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.ComponentModel;
  3. using FastReport.Utils;
  4. using FastReport.Data;
  5. using System.Windows.Forms;
  6. using System.Drawing.Design;
  7. namespace FastReport.Dialog
  8. {
  9. /// <summary>
  10. /// Base class for list box controls such as <b>ListBoxControl</b>, <b>CheckedListBoxControl</b>.
  11. /// </summary>
  12. public abstract partial class ListBoxBaseControl : DataFilterBaseControl
  13. {
  14. private string selectedIndexChangedEvent;
  15. private string measureItemEvent;
  16. private string drawItemEvent;
  17. #region Properties
  18. /// <summary>
  19. /// Occurs when the <b>SelectedIndex</b> property has changed.
  20. /// Wraps the <see cref="System.Windows.Forms.ListBox.SelectedIndexChanged"/> event.
  21. /// </summary>
  22. public event EventHandler SelectedIndexChanged;
  23. /// <summary>
  24. /// Occurs when an owner-drawn ListBox is created and the sizes of the list items are determined.
  25. /// Wraps the <see cref="System.Windows.Forms.ListBox.MeasureItem"/> event.
  26. /// </summary>
  27. public event MeasureItemEventHandler MeasureItem;
  28. /// <summary>
  29. /// Occurs when a visual aspect of an owner-drawn ListBox changes.
  30. /// Wraps the <see cref="System.Windows.Forms.ListBox.DrawItem"/> event.
  31. /// </summary>
  32. public event DrawItemEventHandler DrawItem;
  33. private ListBox ListBox
  34. {
  35. get { return Control as ListBox; }
  36. }
  37. /// <summary>
  38. /// Gets or sets the width of columns in a multicolumn ListBox.
  39. /// Wraps the <see cref="System.Windows.Forms.ListBox.ColumnWidth"/> property.
  40. /// </summary>
  41. [DefaultValue(0)]
  42. [Category("Behavior")]
  43. public int ColumnWidth
  44. {
  45. get { return ListBox.ColumnWidth; }
  46. set { ListBox.ColumnWidth = value; }
  47. }
  48. /// <summary>
  49. /// Gets or sets the drawing mode for the control.
  50. /// Wraps the <see cref="System.Windows.Forms.ListBox.DrawMode"/> property.
  51. /// </summary>
  52. [DefaultValue(DrawMode.Normal)]
  53. [Category("Behavior")]
  54. public virtual DrawMode DrawMode
  55. {
  56. get { return ListBox.DrawMode; }
  57. set { ListBox.DrawMode = value; }
  58. }
  59. /// <summary>
  60. /// Gets or sets the height of an item in the ListBox.
  61. /// Wraps the <see cref="System.Windows.Forms.ListBox.ItemHeight"/> property.
  62. /// </summary>
  63. [Category("Behavior")]
  64. public virtual int ItemHeight
  65. {
  66. get { return ListBox.ItemHeight; }
  67. set { ListBox.ItemHeight = value; }
  68. }
  69. /// <summary>
  70. /// Gets the items of the ListBox.
  71. /// Wraps the <see cref="System.Windows.Forms.ListBox.Items"/> property.
  72. /// </summary>
  73. [Category("Data")]
  74. [Editor("FastReport.TypeEditors.ItemsEditor, FastReport", typeof(UITypeEditor))]
  75. public ListBox.ObjectCollection Items
  76. {
  77. get { return ListBox.Items; }
  78. }
  79. /// <summary>
  80. /// Gets or sets a value indicating whether the ListBox supports multiple columns.
  81. /// Wraps the <see cref="System.Windows.Forms.ListBox.MultiColumn"/> property.
  82. /// </summary>
  83. [DefaultValue(false)]
  84. [Category("Behavior")]
  85. public bool MultiColumn
  86. {
  87. get { return ListBox.MultiColumn; }
  88. set { ListBox.MultiColumn = value; }
  89. }
  90. /// <summary>
  91. /// Gets or sets the method in which items are selected in the ListBox.
  92. /// Wraps the <see cref="System.Windows.Forms.ListBox.SelectionMode"/> property.
  93. /// </summary>
  94. [DefaultValue(SelectionMode.One)]
  95. [Category("Behavior")]
  96. public SelectionMode SelectionMode
  97. {
  98. get { return ListBox.SelectionMode; }
  99. set { ListBox.SelectionMode = value; }
  100. }
  101. /// <summary>
  102. /// Gets or sets a value indicating whether the items in the ListBox are sorted alphabetically.
  103. /// Wraps the <see cref="System.Windows.Forms.ListBox.Sorted"/> property.
  104. /// </summary>
  105. [DefaultValue(false)]
  106. [Category("Behavior")]
  107. public bool Sorted
  108. {
  109. get { return ListBox.Sorted; }
  110. set { ListBox.Sorted = value; }
  111. }
  112. /// <summary>
  113. /// Gets or sets a value indicating whether the ListBox can recognize and expand tab characters when drawing its strings.
  114. /// Wraps the <see cref="System.Windows.Forms.ListBox.UseTabStops"/> property.
  115. /// </summary>
  116. [DefaultValue(true)]
  117. [Category("Behavior")]
  118. public bool UseTabStops
  119. {
  120. get { return ListBox.UseTabStops; }
  121. set { ListBox.UseTabStops = value; }
  122. }
  123. /// <summary>
  124. /// Gets or sets the string that contains all items text.
  125. /// </summary>
  126. [Browsable(false)]
  127. public virtual string ItemsText
  128. {
  129. get { return Converter.IListToString(Items); }
  130. set { Converter.StringToIList(value, Items); }
  131. }
  132. /// <summary>
  133. /// Gets or sets the zero-based index of the currently selected item in a ListBox.
  134. /// Wraps the <see cref="System.Windows.Forms.ListBox.SelectedIndex"/> property.
  135. /// </summary>
  136. [Browsable(false)]
  137. public int SelectedIndex
  138. {
  139. get { return ListBox.SelectedIndex; }
  140. set { ListBox.SelectedIndex = value; }
  141. }
  142. /// <summary>
  143. /// Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.
  144. /// Wraps the <see cref="System.Windows.Forms.ListBox.SelectedIndices"/> property.
  145. /// </summary>
  146. [Browsable(false)]
  147. public ListBox.SelectedIndexCollection SelectedIndices
  148. {
  149. get { return ListBox.SelectedIndices; }
  150. }
  151. /// <summary>
  152. /// Gets or sets the currently selected item in the ListBox.
  153. /// Wraps the <see cref="System.Windows.Forms.ListBox.SelectedItem"/> property.
  154. /// </summary>
  155. [Browsable(false)]
  156. public object SelectedItem
  157. {
  158. get { return ListBox.SelectedItem; }
  159. set { ListBox.SelectedItem = value; }
  160. }
  161. /// <summary>
  162. /// Gets a collection containing the currently selected items in the ListBox.
  163. /// Wraps the <see cref="System.Windows.Forms.ListBox.SelectedItems"/> property.
  164. /// </summary>
  165. [Browsable(false)]
  166. public ListBox.SelectedObjectCollection SelectedItems
  167. {
  168. get { return ListBox.SelectedItems; }
  169. }
  170. /// <summary>
  171. /// Gets or sets a script method name that will be used to handle the
  172. /// <see cref="SelectedIndexChanged"/> event.
  173. /// </summary>
  174. [Category("Events")]
  175. public string SelectedIndexChangedEvent
  176. {
  177. get { return selectedIndexChangedEvent; }
  178. set { selectedIndexChangedEvent = value; }
  179. }
  180. /// <summary>
  181. /// Gets or sets a script method name that will be used to handle the
  182. /// <see cref="MeasureItem"/> event.
  183. /// </summary>
  184. [Category("Events")]
  185. public string MeasureItemEvent
  186. {
  187. get { return measureItemEvent; }
  188. set { measureItemEvent = value; }
  189. }
  190. /// <summary>
  191. /// Gets or sets a script method name that will be used to handle the
  192. /// <see cref="DrawItem"/> event.
  193. /// </summary>
  194. [Category("Events")]
  195. public string DrawItemEvent
  196. {
  197. get { return drawItemEvent; }
  198. set { drawItemEvent = value; }
  199. }
  200. #endregion
  201. #region Private Methods
  202. private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
  203. {
  204. OnSelectedIndexChanged(e);
  205. }
  206. private void ListBox_MeasureItem(object sender, MeasureItemEventArgs e)
  207. {
  208. OnMeasureItem(e);
  209. }
  210. private void ListBox_DrawItem(object sender, DrawItemEventArgs e)
  211. {
  212. OnDrawItem(e);
  213. }
  214. #endregion
  215. #region Protected Methods
  216. /// <inheritdoc/>
  217. protected override void AttachEvents()
  218. {
  219. base.AttachEvents();
  220. ListBox.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);
  221. ListBox.MeasureItem += new MeasureItemEventHandler(ListBox_MeasureItem);
  222. ListBox.DrawItem += new DrawItemEventHandler(ListBox_DrawItem);
  223. }
  224. /// <inheritdoc/>
  225. protected override void DetachEvents()
  226. {
  227. base.DetachEvents();
  228. ListBox.SelectedIndexChanged -= new EventHandler(ListBox_SelectedIndexChanged);
  229. ListBox.MeasureItem -= new MeasureItemEventHandler(ListBox_MeasureItem);
  230. ListBox.DrawItem -= new DrawItemEventHandler(ListBox_DrawItem);
  231. }
  232. /// <inheritdoc/>
  233. protected override void FillData(DataSourceBase dataSource, Column column)
  234. {
  235. Items.Clear();
  236. Items.AddRange(GetListOfData(dataSource, column));
  237. if (Items.Count > 0)
  238. SelectedIndex = 0;
  239. }
  240. #endregion
  241. #region Public Methods
  242. /// <inheritdoc/>
  243. public override void Serialize(FRWriter writer)
  244. {
  245. ListBoxBaseControl c = writer.DiffObject as ListBoxBaseControl;
  246. base.Serialize(writer);
  247. if (ColumnWidth != c.ColumnWidth)
  248. writer.WriteInt("ColumnWidth", ColumnWidth);
  249. if (DrawMode != c.DrawMode)
  250. writer.WriteValue("DrawMode", DrawMode);
  251. if (ItemHeight != c.ItemHeight)
  252. writer.WriteInt("ItemHeight", ItemHeight);
  253. if (MultiColumn != c.MultiColumn)
  254. writer.WriteBool("MultiColumn", MultiColumn);
  255. if (SelectionMode != c.SelectionMode)
  256. writer.WriteValue("SelectionMode", SelectionMode);
  257. if (Sorted != c.Sorted)
  258. writer.WriteBool("Sorted", Sorted);
  259. if (UseTabStops != c.UseTabStops)
  260. writer.WriteBool("UseTabStops", UseTabStops);
  261. if (ItemsText != c.ItemsText)
  262. writer.WriteStr("ItemsText", ItemsText);
  263. if (SelectedIndexChangedEvent != c.SelectedIndexChangedEvent)
  264. writer.WriteStr("SelectedIndexChangedEvent", SelectedIndexChangedEvent);
  265. if (MeasureItemEvent != c.MeasureItemEvent)
  266. writer.WriteStr("MeasureItemEvent", MeasureItemEvent);
  267. if (DrawItemEvent != c.DrawItemEvent)
  268. writer.WriteStr("DrawItemEvent", DrawItemEvent);
  269. }
  270. /// <summary>
  271. /// This method fires the <b>SelectedIndexChanged</b> event and the script code connected to the <b>SelectedIndexChangedEvent</b>.
  272. /// </summary>
  273. /// <param name="e">Event data.</param>
  274. public virtual void OnSelectedIndexChanged(EventArgs e)
  275. {
  276. if (SelectedIndexChanged != null)
  277. SelectedIndexChanged(this, e);
  278. InvokeEvent(SelectedIndexChangedEvent, e);
  279. }
  280. /// <summary>
  281. /// This method fires the <b>MeasureItem</b> event and the script code connected to the <b>MeasureItemEvent</b>.
  282. /// </summary>
  283. /// <param name="e">Event data.</param>
  284. public virtual void OnMeasureItem(MeasureItemEventArgs e)
  285. {
  286. if (MeasureItem != null)
  287. MeasureItem(this, e);
  288. InvokeEvent(MeasureItemEvent, e);
  289. }
  290. /// <summary>
  291. /// This method fires the <b>DrawItem</b> event and the script code connected to the <b>DrawItemEvent</b>.
  292. /// </summary>
  293. /// <param name="e">Event data.</param>
  294. public virtual void OnDrawItem(DrawItemEventArgs e)
  295. {
  296. if (DrawItem != null)
  297. DrawItem(this, e);
  298. InvokeEvent(DrawItemEvent, e);
  299. }
  300. #endregion
  301. }
  302. }