Library tutorials & articles
Collection Controls with Rich Design Time Support
- Introduction
- Starting Off
- Drawing and Layout Logic
- Controlling Serialization
- Adding the Designer
- Adding & Selecting Buttons
- Wrapping Up
Controlling Serialization
Before any changes we make to the Buttons collection will be serialized to code, we need to
add a TypeConverter class and associate it with ColourButton. A TypeConverter helps the serializers
know how to recreate an object that is already instantiated. I'm going to use a very simple
TypeConverter in this example, which simply tells the serializers to use the default, parameterless
constructor.
VB.NET
Friend Class ColourButtonConverter
Inherits TypeConverter
Public Overloads Overrides Function CanConvertTo(ByVal context As _
ITypeDescriptorContext, ByVal destType As Type) As Boolean
If destType Is GetType(InstanceDescriptor) Then
Return True
End If
Return MyBase.CanConvertTo(context, destType)
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As _
ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(InstanceDescriptor) Then
Dim ci As System.Reflection.ConstructorInfo = _
GetType(ColourButton).GetConstructor(System.Type.EmptyTypes)
Return New InstanceDescriptor(ci, Nothing, False)
End If
Return MyBase.ConvertTo(context, culture, value, destType)
End Function
End Class
C#
internal class ColourButtonConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
{
if (destType == typeof(InstanceDescriptor))
return true;
return base.CanConvertTo(context, destType);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destType)
{
if (destType == typeof(InstanceDescriptor))
{
System.Reflection.ConstructorInfo ci =
typeof(ColourButton).GetConstructor(
System.Type.EmptyTypes);
return new InstanceDescriptor(ci, null, false);
}
return base.ConvertTo(context, culture, value, destType);
}
}
We also need to tell the serializers that they have to go in to our Buttons property before they
will even get that far, and we do this with the DesignerSerializationVisibilityAttribute class. Apart
from its name being such an impressive length, all this attribute does it inform the serializers what
to do with our property. We want them to delve in to the collection, so we specify Content.
When we add buttons to the control at design time, save, close the designer and re-open it, the buttons are there again. That's all we have to do with regards to serialization, and it's a big step out of the way.
Related articles
Related discussion
-
Problem after strong naming an assembly
by rinkurathor1 (0 replies)
-
Very slow inserts using SqlCommand.ExecuteNonQuery()
by porchelvi (1 replies)
-
VB.net class to connect to sql database
by senol01 (2 replies)
-
how to select item to datagrid from textbox
by chandradev1 (49 replies)
-
Adobe Flex reaches out to .NET developers
by ranganathanmca (1 replies)
Related podcasts
-
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Applicatie ontwikkelaar binnen Defensie
in Amsterdam (£50K-£90K per annum) -
Business Analist (Openbaar) Vervoer
in Amsterdam (€50K-€90K per annum) -
Application Engineer (VB, .Net, Java) - Standplaats: Utrecht
in Amsterdam (€50K-€90K per annum)
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
GREAT ARTICLE¡¡¡ thank you very, very much, now i can finish my own control.
I could not save my own custom class object, NOW I CAN.
I've tried to compile the code I've downloaded but at run-time (placed in a form on another project) I don't see the buttons I've created at deign-time..
Is it safe to assume value (of complex type) of a property has instance descriptor converter for every component/control you dropped on design surface.
Regards
Phani
This thread is for discussions of Collection Controls with Rich Design Time Support.