ComboBoxControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /// Represents a Windows combo box control.
  11. /// Wraps the <see cref="System.Windows.Forms.ComboBox"/> control.
  12. /// </summary>
  13. public partial class ComboBoxControl : DataFilterBaseControl
  14. {
  15. private ComboBox comboBox;
  16. private string selectedIndexChangedEvent;
  17. private string measureItemEvent;
  18. private string drawItemEvent;
  19. #region Properties
  20. /// <summary>
  21. /// Occurs after the selection has been changed.
  22. /// Wraps the <see cref="System.Windows.Forms.ComboBox.SelectedIndexChanged"/> event.
  23. /// </summary>
  24. public event EventHandler SelectedIndexChanged;
  25. /// <summary>
  26. /// Occurs each time an owner-drawn <b>ComboBox</b> item needs to be drawn and
  27. /// when the sizes of the list items are determined.
  28. /// Wraps the <see cref="System.Windows.Forms.ComboBox.MeasureItem"/> event.
  29. /// </summary>
  30. public event MeasureItemEventHandler MeasureItem;
  31. /// <summary>
  32. /// Occurs when a visual aspect of an owner-drawn <b>ComboBox</b> changes.
  33. /// Wraps the <see cref="System.Windows.Forms.ComboBox.DrawItem"/> event.
  34. /// </summary>
  35. public event DrawItemEventHandler DrawItem;
  36. /// <summary>
  37. /// Gets an internal <b>ComboBox</b>.
  38. /// </summary>
  39. [Browsable(false)]
  40. public ComboBox ComboBox
  41. {
  42. get { return comboBox; }
  43. }
  44. /// <summary>
  45. /// Gets or sets a value indicating whether your code or the operating system will handle drawing of elements in the list.
  46. /// Wraps the <see cref="System.Windows.Forms.ComboBox.DrawMode"/> property.
  47. /// </summary>
  48. [DefaultValue(DrawMode.Normal)]
  49. [Category("Appearance")]
  50. public DrawMode DrawMode
  51. {
  52. get { return ComboBox.DrawMode; }
  53. set { ComboBox.DrawMode = value; }
  54. }
  55. /// <summary>
  56. /// Gets or sets a value specifying the style of the combo box.
  57. /// Wraps the <see cref="System.Windows.Forms.ComboBox.DropDownStyle"/> property.
  58. /// </summary>
  59. [DefaultValue(ComboBoxStyle.DropDown)]
  60. [Category("Appearance")]
  61. public ComboBoxStyle DropDownStyle
  62. {
  63. get { return ComboBox.DropDownStyle; }
  64. set { ComboBox.DropDownStyle = value; }
  65. }
  66. /// <summary>
  67. /// Gets or sets the width of the of the drop-down portion of a combo box.
  68. /// Wraps the <see cref="System.Windows.Forms.ComboBox.DropDownWidth"/> property.
  69. /// </summary>
  70. [Category("Appearance")]
  71. public int DropDownWidth
  72. {
  73. get { return ComboBox.DropDownWidth; }
  74. set { ComboBox.DropDownWidth = value; }
  75. }
  76. /// <summary>
  77. /// Gets or sets the height in pixels of the drop-down portion of the ComboBox.
  78. /// Wraps the <see cref="System.Windows.Forms.ComboBox.DropDownHeight"/> property.
  79. /// </summary>
  80. [Category("Appearance")]
  81. public int DropDownHeight
  82. {
  83. get { return ComboBox.DropDownHeight; }
  84. set { ComboBox.DropDownHeight = value; }
  85. }
  86. /// <summary>
  87. /// Gets or sets the height of an item in the combo box.
  88. /// Wraps the <see cref="System.Windows.Forms.ComboBox.ItemHeight"/> property.
  89. /// </summary>
  90. [Category("Appearance")]
  91. public int ItemHeight
  92. {
  93. get { return ComboBox.ItemHeight; }
  94. set { ComboBox.ItemHeight = value; }
  95. }
  96. /// <summary>
  97. /// Gets a collection of the items contained in this ComboBox.
  98. /// Wraps the <see cref="System.Windows.Forms.ComboBox.Items"/> property.
  99. /// </summary>
  100. [Category("Data")]
  101. [Editor("FastReport.TypeEditors.ItemsEditor, FastReport", typeof(UITypeEditor))]
  102. public ComboBox.ObjectCollection Items
  103. {
  104. get { return ComboBox.Items; }
  105. }
  106. /// <summary>
  107. /// Gets or sets the maximum number of items to be shown in the drop-down portion of the ComboBox.
  108. /// Wraps the <see cref="System.Windows.Forms.ComboBox.MaxDropDownItems"/> property.
  109. /// </summary>
  110. [DefaultValue(8)]
  111. [Category("Appearance")]
  112. public int MaxDropDownItems
  113. {
  114. get { return ComboBox.MaxDropDownItems; }
  115. set { ComboBox.MaxDropDownItems = value; }
  116. }
  117. /// <summary>
  118. /// Gets or sets a value indicating whether the items in the combo box are sorted.
  119. /// Wraps the <see cref="System.Windows.Forms.ComboBox.Sorted"/> property.
  120. /// </summary>
  121. [DefaultValue(false)]
  122. [Category("Behavior")]
  123. public bool Sorted
  124. {
  125. get { return ComboBox.Sorted; }
  126. set { ComboBox.Sorted = value; }
  127. }
  128. /// <summary>
  129. /// Gets or sets the string that contains all items text.
  130. /// </summary>
  131. [Browsable(false)]
  132. public string ItemsText
  133. {
  134. get { return Converter.IListToString(Items); }
  135. set { Converter.StringToIList(value, Items); }
  136. }
  137. /// <summary>
  138. /// Gets or sets the index specifying the currently selected item.
  139. /// Wraps the <see cref="System.Windows.Forms.ComboBox.SelectedIndex"/> property.
  140. /// </summary>
  141. [Browsable(false)]
  142. public int SelectedIndex
  143. {
  144. get { return ComboBox.SelectedIndex; }
  145. set { ComboBox.SelectedIndex = value; }
  146. }
  147. /// <summary>
  148. /// Gets or sets currently selected item in the ComboBox.
  149. /// Wraps the <see cref="System.Windows.Forms.ComboBox.SelectedItem"/> property.
  150. /// </summary>
  151. [Browsable(false)]
  152. public object SelectedItem
  153. {
  154. get { return ComboBox.SelectedItem; }
  155. set { ComboBox.SelectedItem = value; }
  156. }
  157. /// <summary>
  158. /// Gets or sets a script method name that will be used to handle the
  159. /// <see cref="SelectedIndexChanged"/> event.
  160. /// </summary>
  161. [Category("Events")]
  162. public string SelectedIndexChangedEvent
  163. {
  164. get { return selectedIndexChangedEvent; }
  165. set { selectedIndexChangedEvent = value; }
  166. }
  167. /// <summary>
  168. /// Gets or sets a script method name that will be used to handle the
  169. /// <see cref="MeasureItem"/> event.
  170. /// </summary>
  171. [Category("Events")]
  172. public string MeasureItemEvent
  173. {
  174. get { return measureItemEvent; }
  175. set { measureItemEvent = value; }
  176. }
  177. /// <summary>
  178. /// Gets or sets a script method name that will be used to handle the
  179. /// <see cref="DrawItem"/> event.
  180. /// </summary>
  181. [Category("Events")]
  182. public string DrawItemEvent
  183. {
  184. get { return drawItemEvent; }
  185. set { drawItemEvent = value; }
  186. }
  187. #endregion
  188. #region Private Methods
  189. private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
  190. {
  191. OnSelectedIndexChanged(e);
  192. }
  193. private void ComboBox_MeasureItem(object sender, MeasureItemEventArgs e)
  194. {
  195. OnMeasureItem(e);
  196. }
  197. private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
  198. {
  199. OnDrawItem(e);
  200. }
  201. #endregion
  202. #region Protected Methods
  203. /// <inheritdoc/>
  204. protected override void AttachEvents()
  205. {
  206. base.AttachEvents();
  207. ComboBox.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
  208. ComboBox.MeasureItem += new MeasureItemEventHandler(ComboBox_MeasureItem);
  209. ComboBox.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem);
  210. }
  211. /// <inheritdoc/>
  212. protected override void DetachEvents()
  213. {
  214. base.DetachEvents();
  215. ComboBox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
  216. ComboBox.MeasureItem -= new MeasureItemEventHandler(ComboBox_MeasureItem);
  217. ComboBox.DrawItem -= new DrawItemEventHandler(ComboBox_DrawItem);
  218. }
  219. /// <inheritdoc/>
  220. protected override void FillData(DataSourceBase dataSource, Column column)
  221. {
  222. Items.Clear();
  223. Items.AddRange(GetListOfData(dataSource, column));
  224. if (Items.Count > 0)
  225. SelectedIndex = 0;
  226. }
  227. /// <inheritdoc/>
  228. protected override object GetValue()
  229. {
  230. return DropDownStyle == ComboBoxStyle.DropDown ? Text : (string)SelectedItem;
  231. }
  232. #endregion
  233. #region Public Methods
  234. /// <inheritdoc/>
  235. public override void Serialize(FRWriter writer)
  236. {
  237. ComboBoxControl c = writer.DiffObject as ComboBoxControl;
  238. base.Serialize(writer);
  239. if (DrawMode != c.DrawMode)
  240. writer.WriteValue("DrawMode", DrawMode);
  241. if (DropDownStyle != c.DropDownStyle)
  242. writer.WriteValue("DropDownStyle", DropDownStyle);
  243. if (DropDownWidth != c.DropDownWidth)
  244. writer.WriteInt("DropDownWidth", DropDownWidth);
  245. if (DropDownHeight != c.DropDownHeight)
  246. writer.WriteInt("DropDownHeight", DropDownHeight);
  247. if (ItemHeight != c.ItemHeight)
  248. writer.WriteInt("ItemHeight", ItemHeight);
  249. if (MaxDropDownItems != c.MaxDropDownItems)
  250. writer.WriteInt("MaxDropDownItems", MaxDropDownItems);
  251. if (Sorted != c.Sorted)
  252. writer.WriteBool("Sorted", Sorted);
  253. if (ItemsText != c.ItemsText)
  254. writer.WriteStr("ItemsText", ItemsText);
  255. if (SelectedIndexChangedEvent != c.SelectedIndexChangedEvent)
  256. writer.WriteStr("SelectedIndexChangedEvent", SelectedIndexChangedEvent);
  257. if (MeasureItemEvent != c.MeasureItemEvent)
  258. writer.WriteStr("MeasureItemEvent", MeasureItemEvent);
  259. if (DrawItemEvent != c.DrawItemEvent)
  260. writer.WriteStr("DrawItemEvent", DrawItemEvent);
  261. }
  262. /// <inheritdoc/>
  263. public override void OnLeave(EventArgs e)
  264. {
  265. base.OnLeave(e);
  266. if (DropDownStyle == ComboBoxStyle.DropDown)
  267. OnFilterChanged();
  268. }
  269. /// <summary>
  270. /// This method fires the <b>SelectedIndexChanged</b> event and the script code connected to the <b>SelectedIndexChangedEvent</b>.
  271. /// </summary>
  272. /// <param name="e">Event data.</param>
  273. public virtual void OnSelectedIndexChanged(EventArgs e)
  274. {
  275. OnFilterChanged();
  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. /// <summary>
  302. /// Initializes a new instance of the <b>ComboBoxControl</b> class with default settings.
  303. /// </summary>
  304. public ComboBoxControl()
  305. {
  306. comboBox = new ComboBox();
  307. Control = comboBox;
  308. BindableProperty = this.GetType().GetProperty("Text");
  309. }
  310. }
  311. }