MarginsConverter.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // System.Drawing.MarginsConverter.cs
  3. //
  4. // Author:
  5. // Dennis Hayes ([email protected])
  6. // Herve Poussineau ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc
  9. //
  10. using System;
  11. using System.ComponentModel;
  12. using System.Globalization;
  13. using System.Text.RegularExpressions;
  14. namespace System.Drawing.Printing {
  15. /// <summary>
  16. /// Summary description for MarginsConverter.
  17. /// </summary>
  18. public class MarginsConverter : ExpandableObjectConverter {
  19. public MarginsConverter() {
  20. }
  21. #region Methods
  22. public override bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType) {
  23. if (sourceType == typeof(string))
  24. return true;
  25. return base.CanConvertFrom(context, sourceType);
  26. }
  27. public override bool CanConvertTo(ITypeDescriptorContext context,Type destinationType) {
  28. if (destinationType == typeof(string))
  29. return true;
  30. return base.CanConvertTo(context, destinationType);
  31. }
  32. public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,object value) {
  33. if (value is string)
  34. {
  35. if (value == null)
  36. return new Margins();
  37. // format [left];[right];[top];[bottom]
  38. string separator = @"( |\t)*";
  39. separator = separator + ";" + separator;
  40. string regex = @"(?<left>\d+)" + separator + @"(?<right>\d+)" + separator + @"(?<top>\d+)" + separator + @"(?<bottom>\d+)";
  41. Match match = new Regex(regex).Match(value as string);
  42. if (!match.Success)
  43. throw new ArgumentException("value");
  44. int left, right, top, bottom;
  45. try
  46. {
  47. left = int.Parse(match.Groups["left"].Value);
  48. right = int.Parse(match.Groups["right"].Value);
  49. top = int.Parse(match.Groups["top"].Value);
  50. bottom = int.Parse(match.Groups["bottom"].Value);
  51. }
  52. catch (Exception e)
  53. {
  54. throw new ArgumentException("value", e);
  55. }
  56. return new Margins(left, right, top, bottom);
  57. } else
  58. return base.ConvertFrom(context, culture, value);
  59. }
  60. public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,object value,Type destinationType) {
  61. if (destinationType == typeof(string) && value is Margins)
  62. {
  63. Margins source = value as Margins;
  64. string ret = "{0}; {1}; {2}; {3}";
  65. return String.Format(ret, source.Left, source.Right, source.Top, source.Bottom);
  66. } else
  67. return base.ConvertTo(context, culture, value, destinationType);
  68. }
  69. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  70. {
  71. return true;
  72. }
  73. public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
  74. {
  75. try
  76. {
  77. Margins margins = new Margins();
  78. margins.Left = int.Parse(propertyValues["Left"].ToString());
  79. margins.Right = int.Parse(propertyValues["Right"].ToString());
  80. margins.Top = int.Parse(propertyValues["Top"].ToString());
  81. margins.Bottom = int.Parse(propertyValues["Bottom"].ToString());
  82. return margins;
  83. }
  84. catch (Exception)
  85. {
  86. // in case of error, return null
  87. return null;
  88. }
  89. }
  90. #endregion
  91. }
  92. }