O'Reilly logo

.NET Windows Forms in a Nutshell by Matthew Adams, Ian Griffiths

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

TypeConverter

Synopsis

This class provides a means to translate between one type and other representations of that type—typically a string representation. The designer environment uses type converters to translate between types it does not understand (for example, a System.Drawing.Size) and one it can represent in a System.Windows.Forms.PropertyGrid (for example, a string).

One of the quickest and simplest ways to make your custom type available in a designer is to implement a TypeConverter for it and adorn it with a TypeConverterAttribute to bind the appropriate converter. For finer control, you can add the attribute to a particular property to change the type converter for that particular instance of the type.

To implement a TypeConverter you should override the CanConvertFrom(), CanConvertTo( ), ConvertFrom() and ConvertTo() methods. At a minimum, you should implement conversion to and from a string, and you may also want to support InstanceDescriptor to support more complex initialization scenarios in design-time serialization.

If your object is immutable and requires recreation to modify it, you need to override CreateInstance() and GetCreateInstanceSupported(). This will be passed a System.Collections.IDictionary of property name/value pairs and optionally an ITypeDescriptorContext, which you may need to use in the conversion process.

If the type contains properties or you wish to extend it to appear to support properties of its own, you can override the GetProperties() and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required