What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a component that has a collection property in Visual Basic .NET
DescriptionThis example shows how to make a component that has a collection property in Visual Basic .NET.
Keywordscomponent, collection, collection property, VB .NET, TypeConverter, type converter
CategoriesControls, Software Engineering
To allow thue PropertyGrid control to display a class's complex properties, you need to provide a TypeConverter for the property's type. For an introduction to making TypeConverters, see Make a type converter in VB .NET.

For an example that makes a TypeConverter for a collection property, see Make a TypeConverter for a collection in Visual Basic .NET.

This is a good start but if you use these techniques to make a control or component that has a collection property, the Properties window will display the property but will not persist it when you close and reopen the form.

To make the Properties window save and restore the property's value correctly, you must add the DesignerSerializationVisibility attribute to the property and set the attribute's value to Content. That tells the designer to save and restore the property by using its value (content). The following code shows how the Person component defines its Addresses property.

Private m_Addresses As AddressCollection
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public Property Addresses() As AddressCollection
        Return m_Addresses
    End Get
    Set(ByVal Value As AddressCollection)
        m_Addresses = Value
    End Set
End Property
I have no idea why (1) this is so complicated, (2) the PropertyGrid and Properties window can't use reflection to figure this out on their own, (3) the default behavior is to allow you to edit the property in the Properties window but then not save the changes, (4) this is so poorly documented.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.