Introduction

For my last project, I had a requirement where I can use PropertyGrid control. I tried to get information on that, from Microsoft and searching all over Internet, newsgroups, but I could find only a couple of articles from Microsoft. That's it, surprising, and not even a single example in VB.NET that compiles without errors and explains all in and outs of PropertyGrid control.

So, I decided to put all the information I collected, with fully functional working examples in VB.NET (which is my favorite) and let me tell you, it is the most complex control of all controls in VS.NET, yet very powerful and extremely useful, particularly in Custom Controls, and for Application Configuration settings. Visual Studio .NET property browser is also based upon the PropertyGrid.

Its so complex that I have to split this article into two parts. In this Part-I, I will explain how to use the basic properties and System resources, and in Part-II I will cover the advanced topic on using Custom Editors and User defined types.

Description

Open a new Windows application project using VB.NET. By default PropertyGrid is not visible in the Toolbox. So click on Customize Toolbox and add the PropertyGrid Control.

Adding the control, and dropping it on the Designer is pretty much the same like any other control. Fun starts after that. In order to use the PropertyGrid control, you have to provide a class as source to the PropertyGrid. Let's see a small example.

Press F5 to run the project. Let's see the elements in the SimpleProperties.vb class

Imports System.ComponentModel

Required for the Attributes

DefaultPropertyAttribute("Title")

Indicates the default property for the PropertyGrid (i.e. where cursor is located when loaded)

CategoryAttribute("Application")

Category attribute indicating the Category to which the property belongs to. Properties belonging to one category are grouped together.

Browsable(True)

Indicates whether the property is shown in the grid

[ReadOnly](False)

Indicates that the property is read-only or not

BindableAttribute(False)

Indicates whether the property can be bound to a data source or not

DefaultValueAttribute("")

Default value for the property

DesignOnly(False)

If true, it indicates that the property is Read-only at run time

DescriptionAttribute("Enter Title for the application")

Property Description. This description appears in the bottom when you click the property.

Then add the properties that you want to see in the PropertyGrid with right data types. Finally, assign the Class to the PropertyGrid's SelectedObject property. That's all we have to do. Rest is taken care by the PropertyGrid and you see the following output.

Again, PropertyGrid automatically populates Enumerations as drop down list, and system structures as collapsible sub sections.

Adding custom dropdown list

Now let's add a custom dropdown list which shows all states. In order to add a custom dropdown list, create a class that inherits from System.ComponentModel.TypeConverter or any other class, that was derived from TypeConverter class that matches your datatype. (check for System.ComponentModel namespace for all available custom type convert classes) So, for our example we inherit from System.ComponentModel.StringConverter

Displaying custom data types with expandable properties

.NET framework data types like Size, Point when used in PropertyGrid, they are rendered as collapsible list and displays the sub elements. Let's implement a custom data type that duplicates the above functionality. In order to display a custom data type with a collapsible list, we need a TypeConverter, which converts an object type to string type and back to object. Framework also provides an ExpandableObjectConverter class (which is derived from TypeConverter class ) under System.ComponentModel namespace to implement this functionality. Create a class that defines your custom type. I am using Version number as custom type which will expand to Major,Minor,Build,Private parts.

All the above code is like the other examples, except TypeConverter attribute (<TypeConverter(GetType(ApplicationVersionConverter))>) which points to the TypeConvert class, that is used to convert our object to string and back to object. Let's implement the TypeConvert class

Override the ConvertTo method to implement the logic to convert your object back to string in a format you like. This is the value that is displayed for the parent. While converting make sure that the destinationType parameter is a String and that the value is the same type as the class.

In the source code I have a much elaborated example, which mimic's the SQLConnectsting property.

Notes & To-Do features

In the next article I will explain about using custom editors to display properties like the docking, Graphics and tabs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.