Design-Time Integration—Type Converters

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

Design-Time Integration of Windows Forms Components

© 2004 Chris Sells

Type Converters

When you select a component on a design surface, the entries in the Property Browser are rendered from the design-time control instance. When you edit properties in the Property Browser, the component's design-time instance is updated with the new property values. This synchronicity isn't as straightforward as it seems, however, because the Property Browser displays properties only as text, even though the source properties can be of any type. As values shuttle between the Property Browser and the design-time instance, they must be converted back and forth between the string type and the type of the property.

Enter the type converter, the translator droid of .NET, whose main goal in life is to convert between types. For string-to-type conversion, a type converter is used for each property displayed in the Property Browser, as shown in Figure 9.20.


Image:WinFormsCSharp9-20.jpg
Figure 9.20. The Property Browser and Design-Time Conversion


.NET offers the TypeConverter class (from the System.ComponentModel namespace) as the base implementation type converter. .NET also gives you several derivations—including StringConverter, Int32Converter, and DateTimeConverter—that support conversion between common .NET types. If you know the type that needs conversion at compile time, you can create an appropriate converter directly:

// Type is known at compile time
TypeConverter converter = new Int32Converter();

Or, if you don't know the type that needs conversion until run time, let the TypeDescriptor class (from the System.ComponentModel namespace) make the choice for you:

// Don't know the type before run time
object myData = 0;
TypeConverter converter = TypeDescriptor.GetConverter(myData.GetType());

The TypeDescriptor class provides information about a particular type or object, including methods, properties, events, and attributes. TypeDescriptor.GetConverter evaluates a type to determine a suitable TypeConverter based on the following:

1. Checking whether a type is adorned with an attribute that specifies a particular type converter.
2. Comparing the type against the set of built-in type converters.
3. Returning the TypeConverter base if no other type converters are found.

Because the Property Browser is designed to display the properties of any component, it can't know specific property types in advance. Consequently, it relies on TypeDescriptor.GetConverter to dynamically select the most appropriate type converter for each property.

After a type converter is chosen, the Property Browser and the design-time instance can perform the required conversions, using the same fundamental steps as those shown in the following code:

// Create the appropriate type converter
object myData = 0;
TypeConverter converter = TypeDescriptor.GetConverter(myData.GetType());
   
   // Can converter convert int to string?
   if( converter.CanConvertTo(typeof(string)) ) {
     // Convert it
     object intToString = converter.ConvertTo(42, typeof(string));
     }
   
   // Can converter convert string to int?
   if( converter.CanConvertFrom(typeof(string)) ) {
     // Convert it
     object stringToInt = converter.ConvertFrom("42");
     }

When the Property Browser renders itself, it uses the type converter to convert each design-time instance property to a string representation using the following steps:

1. CanConvertTo: Can you convert from the design-time property type to a string?
2. ConvertTo: If so, please convert property value to string.

The string representation of the source value is then displayed at the property's entry in the Property Browser. If the property is edited and the value is changed, the Property Browser uses the next steps to convert the string back to the source property value:

1. CanConvertFrom: Can you convert back to the design-time property type?
2. ConvertFrom: If so, please convert string to property value.

Some intrinsic type converters can do more than just convert between simple types. To demonstrate, let's expose a Face property of type ClockFace, allowing developers to decide how the clock is displayed, including options for Analog, Digital, or Both:

public enum ClockFace {
  Analog = 0,
  Digital = 1,
  Both = 2
  }
 
class ClockControl : Control {
  ClockFace face = ClockFace.Both;
  public ClockFace Face {
    get { ... }
    set { ... }
    }
  ...
  }

TypeDescriptor.GetConverter returns an EnumConverter, which contains the smarts to examine the source enumeration and convert it to a drop-down list of descriptive string values, as shown in Figure 9.21.


Image:WinFormsCSharp9-21.jpg
Figure 9.21. Enumeration Type Displayed in the Property Browser via EnumConverter


Previous_Page_.gif Next_Page_.gif

Personal tools