Parameter.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.ComponentModel;
  3. using FastReport.Utils;
  4. using System.Drawing.Design;
  5. namespace FastReport.Data
  6. {
  7. /// <summary>
  8. /// Represents a report parameter that is used to pass user data to a report.
  9. /// </summary>
  10. /// <remarks>
  11. /// See <see cref="Report.Parameters"/> for details about using parameters.
  12. /// </remarks>
  13. public class Parameter : Base, IParent
  14. {
  15. #region Fields
  16. private Type dataType;
  17. private object value;
  18. private string expression;
  19. private string description;
  20. private ParameterCollection parameters;
  21. #endregion
  22. #region Properties
  23. /// <summary>
  24. /// Gets or sets the name of parameter.
  25. /// </summary>
  26. [MergableProperty(false)]
  27. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  28. [Category("Design")]
  29. [DisplayName("(Name)")]
  30. public new string Name
  31. {
  32. get => base.Name;
  33. set
  34. {
  35. if (value != "" && Report != null)
  36. {
  37. foreach (Parameter p in ParentCollection)
  38. {
  39. if (p != null && p != this && p.Name == value)
  40. {
  41. throw new DuplicateNameException(value);
  42. }
  43. }
  44. }
  45. base.Name = value;
  46. }
  47. }
  48. /// <summary>
  49. /// Gets or sets the type of parameter.
  50. /// </summary>
  51. [TypeConverter(typeof(FastReport.TypeConverters.DataTypeConverter))]
  52. [Category("Data")]
  53. [Editor("FastReport.TypeEditors.DataTypeEditor, FastReport", typeof(UITypeEditor))]
  54. public Type DataType
  55. {
  56. get { return dataType; }
  57. set
  58. {
  59. if (value != null)
  60. dataType = value;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets or sets the value of parameter.
  65. /// </summary>
  66. /// <remarks>
  67. /// You may specify the static value in this property. Note: if the <see cref="Expression"/>
  68. /// property is not empty, it will be calculated and its value will be returned.
  69. /// </remarks>
  70. [Browsable(false)]
  71. public virtual object Value
  72. {
  73. get
  74. {
  75. if (!String.IsNullOrEmpty(Expression) && !Expression.Contains("[" + Name + "]") && Report != null && Report.IsRunning)
  76. value = Report.Calc(Expression);
  77. return value;
  78. }
  79. set
  80. {
  81. this.value = value;
  82. if (value != null)
  83. dataType = value.GetType();
  84. }
  85. }
  86. /// <summary>
  87. /// Gets or sets value of the parameter as a string.
  88. /// </summary>
  89. [Browsable(false)]
  90. public string AsString
  91. {
  92. get
  93. {
  94. object value = Value;
  95. return value == null ? "" : value.ToString();
  96. }
  97. set
  98. {
  99. Expression = "";
  100. if (value != "") this.value = Convert.ChangeType(value, DataType);
  101. }
  102. }
  103. /// <summary>
  104. /// Gets or sets an expression of the parameter.
  105. /// </summary>
  106. /// <remarks>
  107. /// This expression will be calculated each time you access a parameter's <b>Value</b>.
  108. /// </remarks>
  109. [Category("Data")]
  110. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  111. public string Expression
  112. {
  113. get { return expression; }
  114. set { expression = value; }
  115. }
  116. /// <summary>
  117. /// Gets or sets the description of a parameter.
  118. /// </summary>
  119. public string Description
  120. {
  121. get { return description; }
  122. set { description = value; }
  123. }
  124. /// <summary>
  125. /// Gets a collection of nested parameters.
  126. /// </summary>
  127. /// <remarks>
  128. /// Parameters can have child (nested) parameters. To get or set a nested
  129. /// parameter's value, use the <see cref="Report.GetParameter"/> method.
  130. /// </remarks>
  131. [Browsable(false)]
  132. public ParameterCollection Parameters
  133. {
  134. get { return parameters; }
  135. }
  136. /// <summary>
  137. /// Gets the full name of the parameter. This is useful to get the nested parameter's full name.
  138. /// </summary>
  139. [Browsable(false)]
  140. public string FullName
  141. {
  142. get
  143. {
  144. string result = Name;
  145. Parameter parent = Parent as Parameter;
  146. while (parent != null)
  147. {
  148. result = parent.Name + "." + result;
  149. parent = parent.Parent as Parameter;
  150. }
  151. return result;
  152. }
  153. }
  154. /// <summary>
  155. /// This property is not relevant to this class.
  156. /// </summary>
  157. [Browsable(false)]
  158. public new Restrictions Restrictions
  159. {
  160. get { return base.Restrictions; }
  161. set { base.Restrictions = value; }
  162. }
  163. internal ParameterCollection ParentCollection => (Parent as Parameter)?.Parameters ?? Report?.Parameters;
  164. #endregion
  165. #region Public Methods
  166. /// <inheritdoc/>
  167. public override void Assign(Base source)
  168. {
  169. BaseAssign(source);
  170. }
  171. /// <inheritdoc/>
  172. public override void Serialize(FRWriter writer)
  173. {
  174. base.Serialize(writer);
  175. writer.WriteValue("DataType", DataType);
  176. if (String.IsNullOrEmpty(Expression))
  177. writer.WriteStr("AsString", AsString);
  178. if (!String.IsNullOrEmpty(Expression))
  179. writer.WriteStr("Expression", Expression);
  180. if (!String.IsNullOrEmpty(Description))
  181. writer.WriteStr("Description", Description);
  182. }
  183. /// <inheritdoc/>
  184. public override string[] GetExpressions()
  185. {
  186. return new string[] { Expression };
  187. }
  188. internal Parameter Add(string name)
  189. {
  190. if (Parameters.FindByName(name) != null)
  191. throw new Exception("Parameter " + name + " already exists.");
  192. Parameter variable = new Parameter(name);
  193. Parameters.Add(variable);
  194. return variable;
  195. }
  196. #endregion
  197. #region IParent Members
  198. /// <inheritdoc/>
  199. public virtual bool CanContain(Base child)
  200. {
  201. return child is Parameter;
  202. }
  203. /// <inheritdoc/>
  204. public void GetChildObjects(ObjectCollection list)
  205. {
  206. foreach (Parameter v in Parameters)
  207. {
  208. list.Add(v);
  209. }
  210. }
  211. /// <inheritdoc/>
  212. public void AddChild(Base child)
  213. {
  214. Parameters.Add(child as Parameter);
  215. }
  216. /// <inheritdoc/>
  217. public void RemoveChild(Base child)
  218. {
  219. Parameters.Remove(child as Parameter);
  220. }
  221. /// <inheritdoc/>
  222. public int GetChildOrder(Base child)
  223. {
  224. return Parameters.IndexOf(child as Parameter);
  225. }
  226. /// <inheritdoc/>
  227. public void SetChildOrder(Base child, int order)
  228. {
  229. int oldOrder = child.ZOrder;
  230. if (oldOrder != -1 && order != -1 && oldOrder != order)
  231. {
  232. if (order > Parameters.Count)
  233. order = Parameters.Count;
  234. if (oldOrder <= order)
  235. order--;
  236. Parameters.Remove(child as Parameter);
  237. Parameters.Insert(order, child as Parameter);
  238. }
  239. }
  240. /// <inheritdoc/>
  241. public void UpdateLayout(float dx, float dy)
  242. {
  243. // do nothing
  244. }
  245. #endregion
  246. /// <summary>
  247. /// Initializes a new instance of the <see cref="Parameter"/> class with default settings.
  248. /// </summary>
  249. public Parameter() : this("")
  250. {
  251. }
  252. /// <summary>
  253. /// Initializes a new instance of the <see cref="Parameter"/> class with specified name.
  254. /// </summary>
  255. public Parameter(string name)
  256. {
  257. Name = name;
  258. DataType = typeof(string);
  259. expression = "";
  260. description = "";
  261. parameters = new ParameterCollection(this);
  262. SetFlags(Flags.CanCopy | Flags.CanEdit, false);
  263. }
  264. }
  265. }