DateTimePickerControl.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Forms;
  4. using FastReport.Utils;
  5. namespace FastReport.Dialog
  6. {
  7. /// <summary>
  8. /// Represents a Windows control that allows the user to select a date and a time and to display the date and time with a specified format.
  9. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker"/> control.
  10. /// </summary>
  11. public partial class DateTimePickerControl : DataFilterBaseControl
  12. {
  13. private DateTimePicker dateTimePicker;
  14. private string valueChangedEvent;
  15. #region Properties
  16. /// <summary>
  17. /// Occurs after the date has been changed.
  18. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.ValueChanged"/> event.
  19. /// </summary>
  20. public event EventHandler ValueChanged;
  21. /// <summary>
  22. /// Gets an internal <b>DateTimePicker</b>.
  23. /// </summary>
  24. [Browsable(false)]
  25. public DateTimePicker DateTimePicker
  26. {
  27. get { return dateTimePicker; }
  28. }
  29. /// <summary>
  30. /// Gets or sets a value indicating whether the Value property has been set with a valid date/time value and the displayed value is able to be updated.
  31. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.Checked"/> property.
  32. /// </summary>
  33. [DefaultValue(false)]
  34. [Category("Behavior")]
  35. public bool Checked
  36. {
  37. get { return DateTimePicker.Checked; }
  38. set { DateTimePicker.Checked = value; }
  39. }
  40. /// <summary>
  41. /// Gets or sets the custom date/time format string.
  42. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.CustomFormat"/> property.
  43. /// </summary>
  44. [Category("Data")]
  45. public string CustomFormat
  46. {
  47. get { return DateTimePicker.CustomFormat; }
  48. set { DateTimePicker.CustomFormat = value; }
  49. }
  50. /// <summary>
  51. /// Gets or sets the alignment of the drop-down calendar on the DateTimePicker control.
  52. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.DropDownAlign"/> property.
  53. /// </summary>
  54. [DefaultValue(LeftRightAlignment.Left)]
  55. [Category("Appearance")]
  56. public LeftRightAlignment DropDownAlign
  57. {
  58. get { return DateTimePicker.DropDownAlign; }
  59. set { DateTimePicker.DropDownAlign = value; }
  60. }
  61. /// <summary>
  62. /// Gets or sets the format of the date and time displayed in the control.
  63. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.Format"/> property.
  64. /// </summary>
  65. [DefaultValue(DateTimePickerFormat.Long)]
  66. [Category("Data")]
  67. public DateTimePickerFormat Format
  68. {
  69. get { return DateTimePicker.Format; }
  70. set { DateTimePicker.Format = value; }
  71. }
  72. /// <summary>
  73. /// Gets or sets the maximum date and time that can be selected in the control.
  74. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.MaxDate"/> property.
  75. /// </summary>
  76. [Category("Data")]
  77. public DateTime MaxDate
  78. {
  79. get { return DateTimePicker.MaxDate; }
  80. set { DateTimePicker.MaxDate = value; }
  81. }
  82. /// <summary>
  83. /// Gets or sets the minimum date and time that can be selected in the control.
  84. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.MinDate"/> property.
  85. /// </summary>
  86. [Category("Data")]
  87. public DateTime MinDate
  88. {
  89. get { return DateTimePicker.MinDate; }
  90. set { DateTimePicker.MinDate = value; }
  91. }
  92. /// <summary>
  93. /// Gets or sets a value indicating whether a check box is displayed to the left of the selected date.
  94. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.ShowCheckBox"/> property.
  95. /// </summary>
  96. [DefaultValue(false)]
  97. [Category("Appearance")]
  98. public bool ShowCheckBox
  99. {
  100. get { return DateTimePicker.ShowCheckBox; }
  101. set { DateTimePicker.ShowCheckBox = value; }
  102. }
  103. /// <summary>
  104. /// Gets or sets a value indicating whether a spin button control (also known as an up-down control) is used to adjust the date/time value.
  105. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.ShowUpDown"/> property.
  106. /// </summary>
  107. [DefaultValue(false)]
  108. [Category("Appearance")]
  109. public bool ShowUpDown
  110. {
  111. get { return DateTimePicker.ShowUpDown; }
  112. set { DateTimePicker.ShowUpDown = value; }
  113. }
  114. /// <summary>
  115. /// Gets or sets the date/time value assigned to the control.
  116. /// Wraps the <see cref="System.Windows.Forms.DateTimePicker.Value"/> property.
  117. /// </summary>
  118. [Category("Data")]
  119. public DateTime Value
  120. {
  121. get { return DateTimePicker.Value; }
  122. set { DateTimePicker.Value = value; }
  123. }
  124. /// <summary>
  125. /// Gets or sets a script method name that will be used to handle the
  126. /// <see cref="ValueChanged"/> event.
  127. /// </summary>
  128. [Category("Events")]
  129. public string ValueChangedEvent
  130. {
  131. get { return valueChangedEvent; }
  132. set { valueChangedEvent = value; }
  133. }
  134. #endregion
  135. #region Private Methods
  136. private void DateTimePicker_ValueChanged(object sender, EventArgs e)
  137. {
  138. OnValueChanged(e);
  139. }
  140. #endregion
  141. #region Protected Methods
  142. /// <inheritdoc/>
  143. protected override void AttachEvents()
  144. {
  145. base.AttachEvents();
  146. DateTimePicker.ValueChanged += DateTimePicker_ValueChanged;
  147. }
  148. /// <inheritdoc/>
  149. protected override void DetachEvents()
  150. {
  151. base.DetachEvents();
  152. DateTimePicker.ValueChanged -= DateTimePicker_ValueChanged;
  153. }
  154. /// <inheritdoc/>
  155. protected override object GetValue()
  156. {
  157. if (Format == DateTimePickerFormat.Long || Format == DateTimePickerFormat.Short)
  158. return new DateTime(Value.Year, Value.Month, Value.Day);
  159. return Value;
  160. }
  161. #endregion
  162. #region Public Methods
  163. /// <inheritdoc/>
  164. public override void Serialize(FRWriter writer)
  165. {
  166. DateTimePickerControl c = writer.DiffObject as DateTimePickerControl;
  167. base.Serialize(writer);
  168. if (Checked != c.Checked)
  169. writer.WriteBool("Checked", Checked);
  170. if (CustomFormat != c.CustomFormat)
  171. writer.WriteStr("CustomFormat", CustomFormat);
  172. if (DropDownAlign != c.DropDownAlign)
  173. writer.WriteValue("DropDownAlign", DropDownAlign);
  174. if (Format != c.Format)
  175. writer.WriteValue("Format", Format);
  176. if (MaxDate != c.MaxDate)
  177. writer.WriteValue("MaxDate", MaxDate);
  178. if (MinDate != c.MinDate)
  179. writer.WriteValue("MinDate", MinDate);
  180. if (ShowCheckBox != c.ShowCheckBox)
  181. writer.WriteBool("ShowCheckBox", ShowCheckBox);
  182. if (ShowUpDown != c.ShowUpDown)
  183. writer.WriteBool("ShowUpDown", ShowUpDown);
  184. if (Value != c.Value)
  185. writer.WriteValue("Value", Value);
  186. if (ValueChangedEvent != c.ValueChangedEvent)
  187. writer.WriteStr("ValueChangedEvent", ValueChangedEvent);
  188. }
  189. /// <summary>
  190. /// This method fires the <b>ValueChanged</b> event and the script code connected to the <b>ValueChangedEvent</b>.
  191. /// </summary>
  192. /// <param name="e">Event data.</param>
  193. public virtual void OnValueChanged(EventArgs e)
  194. {
  195. OnFilterChanged();
  196. if (ValueChanged != null)
  197. ValueChanged(this, e);
  198. InvokeEvent(ValueChangedEvent, e);
  199. }
  200. #endregion
  201. /// <summary>
  202. /// Initializes a new instance of the <b>DateTimePickerControl</b> class with default settings.
  203. /// </summary>
  204. public DateTimePickerControl()
  205. {
  206. dateTimePicker = new DateTimePicker();
  207. Control = dateTimePicker;
  208. BindableProperty = this.GetType().GetProperty("Value");
  209. }
  210. }
  211. }