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. 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 DateTimeConverterthat 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 Dim converter As TypeConverter = 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 Dim myData As Object = 0 Dim converter As TypeConverter = 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:
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 Dim myData As Object = 0 Dim converter As TypeConverter = TypeDescriptor.GetConverter(myData.GetType()) ' Can converter convert integer to string? If converter.CanConvertTo(GetType(String)) Then ' Convert it Dim intToString As Object = converter.ConvertTo(42, _ GetType(String)) End If ' Can converter convert string to integer? If converter.CanConvertFrom(GetType(String)) Then ' Convert it Dim stringToInt As Object = converter.ConvertFrom("42") End If 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:
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:
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 End Enum Class ClockControl Inherits Control Dim face As ClockFace = ClockFace.Both Public Property Face() As ClockFace Get ... End Get Set ... End Set End Property End Class 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. Figure 9.21. Enumeration Type Displayed in the Property Browser via EnumConverter
Custom Type ConvertersAlthough the built-in type converters are useful, they aren't enough if your component or control exposes properties based on custom types, such as the clock control's HourHand, MinuteHand, and SecondHand properties, shown here: Public Class Hand Public Sub New(mycolor As Color, width As Integer) Me.mycolor = mycolor Me.width = mywidth End Sub Public Property Color() As Color Get Return mycolor End Get Set mycolor = Value End Set End Property Public Property Width() as Integer Get Return mywidth End Get Set mywidth = Value End Set End Property Dim mycolor As Color = Color.Black Dim mywidth As Integer = 1 End Class Public Class ClockControl Inherits Control Public Property HourHand() As Hand Public Property MinuteHand() As Hand Public Property SecondHand() As Hand End Class The idea is to give developers the option to pretty up the clock's hands with color and width values. Without a custom type converter, [5] the unfortunate result is shown in Figure 9.22.
Figure 9.22. Complex Properties in the Property Browser
Just as the Property Browser can't know which types it will be displaying, .NET can't know which custom types you'll be developing. Consequently, there aren't any type converters capable of handling them. However, you can hook into the type converter infrastructure to provide your own. Building a custom type converter starts by deriving from the TypeConverter base class: Public Class HandConverter Inherits TypeConverter ... End Class To support conversion, HandConverter must override CanConvertFrom, ConvertTo, and ConvertFrom: Public Class HandConverter Inherits TypeConverter Public Overrides Function CanConvertFrom(_ context As ITypeDescriptorContext, _ sourceType As Type) As Boolean ... End Function Public Overrides Function ConvertFrom(_ context As ITypeDescriptorContext, info As CultureInfo, _ value As Object) As Object ... End Function Public Overrides Function ConvertTo(context As _ ITypeDescriptorContext, _ culture As CultureInfo, value As Object, _ destinationType As Type) As Object ... End Function End Class CanConvertFrom lets clients know what it can convert from. In this case, HandConverter reports that it can convert from a string type to a Hand type: Public Overrides Function CanConvertFrom( _ context As ITypeDescriptorContext, sourceType As Type) As Boolean ' We can convert from a string to a Hand type If sourceType = GetType(String) Then Return True Return MyBase.CanConvertFrom(context, sourceType) End Function Whether the string type is in the correct format is left up to ConvertFrom, which actually performs the conversion. HandConverter expects a multivalued string. It splits this string into its atomic values and then uses it to instantiate a Hand object: Public Overrides Function ConvertFrom( _ context As ITypeDescriptorContext, _ info As CultureInfo, value As Object) _ As Object ' If converting from a string If TypeOf value Is String Then ' Build a Hand type Try ' Get Hand properties Dim propertyList As String = CStr(value) Dim properties As String() = propertyList.Split(";"c) Return New Hand(Color.FromName(properties(0).Trim()), _ Integer.Parse(properties(1)) Catch End Try Throw New ArgumentException("The arguments were not valid.") End If Return MyBase.ConvertFrom(context, info, value) End Function ConvertTo converts from a Hand type back to a string: Public Overloads Overrides Function ConvertTo( _ context As ITypeDescriptorContext, culture As CultureInfo, _ value As Object, destinationType As Type) As Object ' If source value is a Hand type If TypeOf value Is Hand Then ' Convert to String If destinationType = GetType(String) Then Dim myhand As Hand = CType(value, Hand) Dim mycolor As String = _ Iif(myhand.Color.IsNamedColor, _ myhand.Color.Name, _ Myhand.Color.R & ", " & myhand.Color.G & _ ", " & myhand.Color.B) Return String.Format("{0}; {1}", mycolor, _ myhand.Width.ToString()) End If End If Return MyBase.ConvertTo(context, culture, value, destinationType) End Function You may have noticed that HandConverter doesn't implement a CanConvertTo override. The base implementation of TypeConverter.CanConvertTo returns a Boolean value of true when queried for its ability to convert to a string type. Because this is the right behavior for HandConverter (and for most other custom type converters), there's no need to override it. When the Property Browser looks for a custom type converter, it looks at each property for a TypeConverterAttribute: Public Class ClockControl Inherits Control ... <TypeConverter(GetType(HandConverter))> _ Public Property HourHand() As Hand <TypeConverter(GetType(HandConverter))> _ Public Property MinuteHand() As Hand <TypeConverter(GetType(HandConverter))> _ Public Property SecondHand() As Hand ... End Class However, this is somewhat cumbersome, so it's simpler to decorate the type itself with TypeConverterAttribute: <TypeConverter(GetType(HandConverter))> _ Public Class Hand ... End Class Public Class ClockControl Inherits Control Public Property HourHand() As Hand Public Property MinuteHand() As Hand Public Property SecondHand() As Hand ... End Class Figure 9.23 shows the effect of the custom HandConverter type converter. Figure 9.23. HandConverter in Action (See Plate 23)
Expandable Object ConverterAlthough using the UI shown in Figure 9.23 is better than not being able to edit the property at all, there are still ways it can be improved. For instance, put yourself in a developer's shoes. Although it might be obvious what the first part of the property is, it's disappointing not to be able to pick the color from one of those pretty drop-down color pickers. And what is the second part of the property meant to be? Length, width, degrees, something else? As an example of what you'd like to see, the Font type supports browsing and editing of its subproperties , as shown in Figure 9.24. Figure 9.24. Expanded Property Value
This ability to expand a property of a custom type makes it a lot easier to understand what the property represents and what sort of values you need to provide. To allow subproperty editing, you simply change the base type from TypeConverter to ExpandableObjectConverter (from the System.ComponentModel namespace): Public Class HandConverter Inherits ExpandableObjectConverter ... End Class This change gives you multivalue and nested property editing support, as shown in Figure 9.25. Figure 9.25. HandConverter Derived from ExpandableObjectConverter
Although you don't have to write any code to make this property expandable, you must write a little code to fix an irksome problem: a delay in property updating. In expanded mode, a change to the root property value is automatically reflected in the nested property value list. This occurs because the root property entry refers to the design-time property instance, whereas its nested property values refer to the design-time instance's properties directly, as illustrated in Figure 9.26. Figure 9.26. Relationship between Root and Nested Properties and Design-Time Property Instance
When the root property is edited, the Property Browser calls HandConverter.ConvertFrom to convert the Property Browser's string entry to a new SecondHand instance, and that results in a refresh of the Property Browser. However, changing the nested values only changes the current instance's property values, rather than creating a new instance, and that doesn't result in an immediate refresh of the root property. TypeConverters offer a mechanism you can use to force the creation of a new instance whenever instance property values change, something you achieve by overriding GetCreateInstanceSupported and CreateInstance. The GetCreateInstanceSupported method returns a Boolean indicating whether this support is available and, if it is, calls CreateInstance to implement it: Public Class HandConverter Inherits ExpandableObjectConverter Public Overloads Overrides Function GetCreateInstanceSupported( _ context As ITypeDescriptorContext) As Boolean ' Always force a new instance Return True End Function Public Overloads Overrides Function CreateInstance( _ context As ITypeDescriptorContext, _ propertyValues As IDictionary) _ As Object ' Use the dictionary to create a new instance Return New Hand(CType(propertyValues("Color"), Color), _ CInt(propertyValues("Width"))) End Function ... End Class If GetCreateInstanceSupported returns true, then CreateInstance will be used to create a new instance whenever any of the subproperties of an expandable object are changed. The propertyValues argument to CreateInstance provides a set of name/value pairs for the current values of the object's subproperties, and you can use them to construct a new instance. Custom Type Code Serialization with TypeConvertersAlthough the Hand type now plays nicely with the Property Browser, it doesn't yet play nicely with code serialization. In fact, at this point it's not being serialized to InitializeComponent at all. To enable serialization of properties exposing complex types, you must expose a public ShouldSerialize<PropertyName> method that returns a Boolean: Public Class ClockControl Inherits Control Public Property SecondHand() As Hand Function ShouldSerializeSecondHand() As Boolean ' Only serialize nondefault values Return (secondHand.Color <> Color.Red And _ secondHand.Width <> 1) End Function ... End Class Internally, the Designer looks for a method named ShouldSerialize- <PropertyName> to ask whether the property should be serialized. From the Designer's point of view, it doesn't matter whether your ShouldSerialize- <PropertyName> is public or private, but choosing private removes it from client visibility. To programmatically implement the Property Browser reset functionality, you use the Reset<PropertyName> method: Public Property SecondHand() As Hand Sub ResetSecondHand() SecondHand = New Hand(Color.Red, 1) End Sub Implementing ShouldSerialize lets the design-time environment know whether the property should be serialized, but you also need to write custom code to help assist in the generation of appropriate InitializeComponent code. Specifically, the Designer needs an instance descriptor , which provides the information needed to create an instance of a particular type. The code serializer gets an InstanceDescriptor object for a Hand by asking the Hand type converter: Public Class HandConverter Inherits ExpandableObjectConverter Public Overloads Overrides Function CanConvertTo( _ context As ITypeDescriptorContext, destinationType As Type) ' We can be converted to an InstanceDescriptor If destinationType Is GetType(InstanceDescriptor) Then Return True Return MyBase.CanConvertTo(context, destinationType) End Function Public Overloads Overrides Function ConvertTo(_ context As ITypeDescriptorContext, culture As CultureInfo, _ value As Object, destinationType As Type) As Object If TypeOf value Is Hand Then ' Convert to InstanceDescriptor If destinationType Is GetType(InstanceDescriptor) Then Dim myhand As Hand = CType(value, Hand) Dim properties(1) as Object Dim types(1) As Type ' Color types(0) = GetType(Color) properties(0) = myhand.Color ' Width types(1) = GetType(Integer) properties(1) = myhand.Width ' Build constructor Dim ci As ConstructorInfo = _ GetType(Hand).GetConstructor(types) Return New InstanceDescriptor(ci, properties) End If ... End If Return MyBase.ConvertTo(context, culture, _ value, destinationType) End Function End Class To be useful, an instance descriptor requires two pieces of information. First, it needs to know what the constructor looks like. Second, it needs to know which property values should be used if the object is instantiated . The former is described by the ConstructorInfo type, and the latter is simply an array of values, which should be in constructor parameter order. After the control is rebuilt and assuming that ShouldSerialize<PropertyName> permits , all Hand type properties will be serialized using the information provided by the HandConverter-provided InstanceDescriptor: Public Class ClockControlHostForm Inherits Form ... Sub InitializeComponent() ... Me.clockControl1.HourHand = _ New ClockControlLibrary.Hand(System.Drawing.Color.Black, 2) ... End Sub End Class Type converters provide all kinds of help for the Property Browser and the Designer to display, convert, and serialize properties of custom types for components that use such properties. |