CustomFormat.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. //If value is "00:00:00"() and it can be converted to DateTime
  56. if (value is TimeSpan && DateTime.TryParse(value.ToString(), out DateTime dateTime))
  57. return String.Format("{0:" + Format + "}", dateTime);
  58. return String.Format("{0:" + Format + "}", value);
  59. }
  60. internal override string GetSampleValue()
  61. {
  62. return "";
  63. }
  64. internal override void Serialize(FRWriter writer, string prefix, FormatBase format)
  65. {
  66. base.Serialize(writer, prefix, format);
  67. CustomFormat c = format as CustomFormat;
  68. if (c == null || Format != c.Format)
  69. writer.WriteStr(prefix + "Format", Format);
  70. }
  71. #endregion
  72. /// <summary>
  73. /// Initializes a new instance of the <b>CustomFormat</b> class with default settings.
  74. /// </summary>
  75. public CustomFormat()
  76. {
  77. Format = "G";
  78. }
  79. }
  80. }