2 // System.Drawing.RectangleConverter.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Jordi Mas (jordi@ximian.com)
7 // Ravindra (rkumar@novell.com)
9 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
14 using System.ComponentModel;
15 using System.Collections;
16 using System.Globalization;
19 namespace System.Drawing
22 /// Summary description for RectangleConverter.
24 public class RectangleConverter : TypeConverter
26 public RectangleConverter ()
30 public override bool CanConvertFrom (ITypeDescriptorContext context,
33 if (sourceType == typeof (string))
36 return base.CanConvertFrom (context, sourceType);
39 public override bool CanConvertTo (ITypeDescriptorContext context,
42 if (destinationType == typeof (string))
45 return base.CanConvertTo (context, destinationType);
48 public override object ConvertFrom (ITypeDescriptorContext context,
52 string s = value as string;
54 return base.ConvertFrom (context, culture, value);
56 string [] subs = s.Split (',');
58 throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"x,y,Width,Height.\"");
60 int x = Int32.Parse (subs [0]);
61 int y = Int32.Parse (subs [1]);
62 int width = Int32.Parse (subs [2]);
63 int height = Int32.Parse (subs [3]);
65 return new Rectangle (x, y, width, height);
68 public override object ConvertTo (ITypeDescriptorContext context,
73 if ((destinationType == typeof (string)) && (value is Rectangle)) {
74 Rectangle rect = (Rectangle) value;
75 StringBuilder sb = new StringBuilder ();
76 sb.Append (rect.X); sb.Append (", ");
77 sb.Append (rect.Y); sb.Append (", ");
78 sb.Append (rect.Width); sb.Append (", ");
79 sb.Append (rect.Height);
80 return sb.ToString ();
83 return base.ConvertTo (context, culture, value, destinationType);
86 public override object CreateInstance (ITypeDescriptorContext context,
87 IDictionary propertyValues)
89 int x = (int) propertyValues ["X"];
90 int y = (int) propertyValues ["Y"];
91 int width = (int) propertyValues ["Width"];
92 int height = (int) propertyValues ["Height"];
94 return new Rectangle (x, y, width, height);
97 public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
103 public override PropertyDescriptorCollection GetProperties (
104 ITypeDescriptorContext context,
105 object value, Attribute[] attributes)
107 throw new NotImplementedException ();
110 public override bool GetPropertiesSupported (ITypeDescriptorContext context)