NumericUpDownControl.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Forms;
  4. using FastReport.Utils;
  5. using FastReport.Data;
  6. namespace FastReport.Dialog
  7. {
  8. /// <summary>
  9. /// Represents a Windows spin box (also known as an up-down control) that displays numeric values.
  10. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown"/> control.
  11. /// </summary>
  12. public partial class NumericUpDownControl : DataFilterBaseControl
  13. {
  14. private NumericUpDown numericUpDown;
  15. private string valueChangedEvent;
  16. #region Properties
  17. /// <summary>
  18. /// Occurs when the Value property has been changed in some way.
  19. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.ValueChanged"/> event.
  20. /// </summary>
  21. public event EventHandler ValueChanged;
  22. /// <summary>
  23. /// Gets an internal <b>NumericUpDown</b>.
  24. /// </summary>
  25. [Browsable(false)]
  26. public NumericUpDown NumericUpDown
  27. {
  28. get { return numericUpDown; }
  29. }
  30. /// <summary>
  31. /// Gets or sets the number of decimal places to display in the up-down control.
  32. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.DecimalPlaces"/> property.
  33. /// </summary>
  34. [DefaultValue(0)]
  35. [Category("Data")]
  36. public int DecimalPlaces
  37. {
  38. get { return NumericUpDown.DecimalPlaces; }
  39. set { NumericUpDown.DecimalPlaces = value; }
  40. }
  41. /// <summary>
  42. /// Gets or sets a value indicating whether the up-down control should display the value it contains in hexadecimal format.
  43. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.Hexadecimal"/> property.
  44. /// </summary>
  45. [DefaultValue(false)]
  46. [Category("Appearance")]
  47. public bool Hexadecimal
  48. {
  49. get { return NumericUpDown.Hexadecimal; }
  50. set { NumericUpDown.Hexadecimal = value; }
  51. }
  52. /// <summary>
  53. /// Gets or sets the value to increment or decrement the up-down control when the up or down buttons are clicked.
  54. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.Increment"/> property.
  55. /// </summary>
  56. [DefaultValue(1f)]
  57. [Category("Data")]
  58. public float Increment
  59. {
  60. get { return (float)NumericUpDown.Increment; }
  61. set { NumericUpDown.Increment = (decimal)value; }
  62. }
  63. /// <summary>
  64. /// Gets or sets the maximum value for the up-down control.
  65. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.Maximum"/> property.
  66. /// </summary>
  67. [DefaultValue(typeof(decimal), "100")]
  68. [Category("Data")]
  69. public decimal Maximum
  70. {
  71. get { return NumericUpDown.Maximum; }
  72. set { NumericUpDown.Maximum = value; }
  73. }
  74. /// <summary>
  75. /// Gets or sets the minimum value for the up-down control.
  76. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.Minimum"/> property.
  77. /// </summary>
  78. [DefaultValue(typeof(decimal), "0")]
  79. [Category("Data")]
  80. public decimal Minimum
  81. {
  82. get { return NumericUpDown.Minimum; }
  83. set { NumericUpDown.Minimum = value; }
  84. }
  85. /// <summary>
  86. /// Gets or sets a value indicating whether a thousands separator is displayed in the up-down control when appropriate.
  87. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.ThousandsSeparator"/> property.
  88. /// </summary>
  89. [DefaultValue(false)]
  90. [Category("Data")]
  91. public bool ThousandsSeparator
  92. {
  93. get { return NumericUpDown.ThousandsSeparator; }
  94. set { NumericUpDown.ThousandsSeparator = value; }
  95. }
  96. /// <summary>
  97. /// Gets or sets the value assigned to the up-down control.
  98. /// Wraps the <see cref="System.Windows.Forms.NumericUpDown.Value"/> property.
  99. /// </summary>
  100. [Category("Data")]
  101. public decimal Value
  102. {
  103. get { return NumericUpDown.Value; }
  104. set { NumericUpDown.Value = value; }
  105. }
  106. /// <summary>
  107. /// Gets or sets a script method name that will be used to handle the
  108. /// <see cref="ValueChanged"/> event.
  109. /// </summary>
  110. [Category("Events")]
  111. public string ValueChangedEvent
  112. {
  113. get { return valueChangedEvent; }
  114. set { valueChangedEvent = value; }
  115. }
  116. #endregion
  117. #region Private Methods
  118. private void NumericUpDown_ValueChanged(object sender, EventArgs e)
  119. {
  120. OnValueChanged(e);
  121. }
  122. #endregion
  123. #region Protected Methods
  124. /// <inheritdoc/>
  125. protected override void AttachEvents()
  126. {
  127. base.AttachEvents();
  128. NumericUpDown.ValueChanged += new EventHandler(NumericUpDown_ValueChanged);
  129. }
  130. /// <inheritdoc/>
  131. protected override void DetachEvents()
  132. {
  133. base.DetachEvents();
  134. NumericUpDown.ValueChanged -= new EventHandler(NumericUpDown_ValueChanged);
  135. }
  136. /// <inheritdoc/>
  137. protected override object GetValue()
  138. {
  139. object value = Value;
  140. Column dataColumn = DataHelper.GetColumn(Report.Dictionary, DataColumn);
  141. if (dataColumn != null)
  142. value = Convert.ChangeType((decimal)value, dataColumn.DataType);
  143. return value;
  144. }
  145. #endregion
  146. #region Public Methods
  147. /// <inheritdoc/>
  148. public override void Serialize(FRWriter writer)
  149. {
  150. NumericUpDownControl c = writer.DiffObject as NumericUpDownControl;
  151. base.Serialize(writer);
  152. if (DecimalPlaces != c.DecimalPlaces)
  153. writer.WriteInt("DecimalPlaces", DecimalPlaces);
  154. if (Hexadecimal != c.Hexadecimal)
  155. writer.WriteBool("Hexadecimal", Hexadecimal);
  156. if (Increment != c.Increment)
  157. writer.WriteFloat("Increment", Increment);
  158. if (Maximum != c.Maximum)
  159. writer.WriteValue("Maximum", Maximum);
  160. if (Minimum != c.Minimum)
  161. writer.WriteValue("Minimum", Minimum);
  162. if (ThousandsSeparator != c.ThousandsSeparator)
  163. writer.WriteBool("ThousandsSeparator", ThousandsSeparator);
  164. if (Value != c.Value)
  165. writer.WriteValue("Value", Value);
  166. if (ValueChangedEvent != c.ValueChangedEvent)
  167. writer.WriteStr("ValueChangedEvent", ValueChangedEvent);
  168. }
  169. /// <inheritdoc/>
  170. public override void OnLeave(EventArgs e)
  171. {
  172. base.OnLeave(e);
  173. OnFilterChanged();
  174. }
  175. /// <summary>
  176. /// This method fires the <b>ValueChanged</b> event and the script code connected to the <b>ValueChangedEvent</b>.
  177. /// </summary>
  178. /// <param name="e">Event data.</param>
  179. public virtual void OnValueChanged(EventArgs e)
  180. {
  181. if (ValueChanged != null)
  182. ValueChanged(this, e);
  183. InvokeEvent(ValueChangedEvent, e);
  184. }
  185. #endregion
  186. /// <summary>
  187. /// Initializes a new instance of the <b>NumericUpDownControl</b> class with default settings.
  188. /// </summary>
  189. public NumericUpDownControl()
  190. {
  191. numericUpDown = new NumericUpDown();
  192. Control = numericUpDown;
  193. BindableProperty = this.GetType().GetProperty("Value");
  194. }
  195. }
  196. }