CustomFormat.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using FastReport.Utils;
  6. namespace FastReport.Format
  7. {
  8. /// <summary>
  9. /// Represents a format that uses the <b>Format</b> string to display values.
  10. /// </summary>
  11. public class CustomFormat : FormatBase
  12. {
  13. #region Fields
  14. private string format;
  15. #endregion
  16. #region Properties
  17. /// <summary>
  18. /// Gets or sets a format string.
  19. /// </summary>
  20. /// <remarks>
  21. /// Default format is "G". For example, if you want to format a date, use the following
  22. /// format string: "MM/dd/yyyy". See the <b>System.String.Format</b> method for list
  23. /// of possible format strings.
  24. /// </remarks>
  25. public string Format
  26. {
  27. get { return format; }
  28. set { format = value; }
  29. }
  30. #endregion
  31. #region Public Methods
  32. /// <inheritdoc/>
  33. public override FormatBase Clone()
  34. {
  35. CustomFormat result = new CustomFormat();
  36. result.Format = Format;
  37. return result;
  38. }
  39. /// <inheritdoc/>
  40. public override bool Equals(object obj)
  41. {
  42. CustomFormat f = obj as CustomFormat;
  43. return f != null && Format == f.Format;
  44. }
  45. /// <inheritdoc/>
  46. public override int GetHashCode()
  47. {
  48. return base.GetHashCode();
  49. }
  50. /// <inheritdoc/>
  51. public override string FormatValue(object value)
  52. {
  53. if (value is Variant)
  54. value = ((Variant)value).Value;
  55. return String.Format("{0:" + Format + "}", value);
  56. }
  57. internal override string GetSampleValue()
  58. {
  59. return "";
  60. }
  61. internal override void Serialize(FRWriter writer, string prefix, FormatBase format)
  62. {
  63. base.Serialize(writer, prefix, format);
  64. CustomFormat c = format as CustomFormat;
  65. if (c == null || Format != c.Format)
  66. writer.WriteStr(prefix + "Format", Format);
  67. }
  68. #endregion
  69. /// <summary>
  70. /// Initializes a new instance of the <b>CustomFormat</b> class with default settings.
  71. /// </summary>
  72. public CustomFormat()
  73. {
  74. Format = "G";
  75. }
  76. }
  77. }