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 ...

Get .NET Windows Forms in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.