I had written a control with a number of properties, most of which showed up automatically in the Visual Studio Properties window. Simple data types were not a problem, nor were arrays: Visual Studio automatically opens up a window that lets the user edit the array elements. However, class or structure instances were not editable […]

This article was posted by Independent Software, a website and database application development company based in Maputo, Mozambique. Our website offers regular write-ups on technical and design issues, ranging from details at code level to 3D Studio Max rendering. Read more about Independent Software's philosophy, or get in touch with Independent Software.

I had written a control with a number of properties, most of which showed up automatically in the Visual Studio Properties window. Simple data types were not a problem, nor were arrays: Visual Studio automatically opens up a window that lets the user edit the array elements. However, class or structure instances were not editable out of the box: they were shown greyed-out in the Properties window.

The solution to this lies in that Visual Studio needs to know how the subclass or subproperty should be edited. An example of this is the Padding property that all controls share: it’s a structure with a couple of properties (Left, Right, Bottom, Top and All), and in the Properties window you can expand the Padding property so you can edit any of these properties individually. This is precisely what I wanted for my substructures.

It’s easy to convince Visual Studio .NET to give you this behavior by adding a TypeConverter attribute to your structure, like so:

ExpandableObjectConverter is already implemented for you, as are some others. If you want different behavior in the Properties window, you can implement your own TypeConverter.

Leave a Reply

Your email address will not be published. Required fields are marked *